gpt4 book ai didi

c++ - 循环永无止境??~需要帮助。 C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:33:03 25 4
gpt4 key购买 nike

问题:

给定这样的输入:

int myintsA[]={1,3,3,2,2,5,5,5,4};
int myintsB[]={0,9,8,6,7,3,3,4};

找到相同的元素,输出。当它们同时出现 2 次时。输出为 2 倍。

例如:
输出: {3,3,4}

   #include <iostream>
#include <vector>
#include <list>
#include <ext/hash_map>

using namespace __gnu_cxx;
using namespace std;

list<int> findDup(const vector<int>& A ,const vector<int>& B)
{
list<int> idx;
std::vector<int>::size_type i = 0;
std::vector<int>::size_type j = 0;
while(i < A.size() && j < B.size()) {
if (A[i] == B[j])
{
idx.push_back(A[i]);
i++;
j++;
}
else if(A[i] < B[j])
{
i++;
}
else if (A[i] > B[j])
{
j++;
}
}
return idx;

}

int main()
{
int myintsA[]={1,3,2,2,5,5,5,4};
int myintsB[]={0,9,8,6,7,3,3,4};

vector<int> myvectorA (myintsA, myintsA + sizeof(myintsA) / sizeof(int) );
vector<int> myvectorB (myintsB, myintsB + sizeof(myintsB) / sizeof(int) );


sort(myvectorA.begin(),myvectorA.end());
sort(myvectorB.begin(),myvectorB.end());

list<int> result = findDup(myvectorA, myvectorB);
for(list<int>::iterator iter = result.begin(); iter!=result.end();++iter)
{
printf("%c",*iter);
}
return 0;
}

但是我的程序好像有问题。需要帮忙!谢谢!

最佳答案

这是之前发布的代码的工作版本:

#include <iostream>
#include <vector>
#include <list>
#include <algorithm> //It's better to use the standard <algorithm> header here for sort.
//using <ext/hash_map> just for the sort functionality is not a great idea because it makes the code less clear and also creates portability issues.
using namespace __gnu_cxx;
using namespace std;

list<int> findDup(const vector<int>& A ,const vector<int>& B)
{
list<int> idx;
std::vector<int>::size_type i = 0;
std::vector<int>::size_type j = 0;
while(i < A.size() && j < B.size()) { //as pointed out before this is the source of the error
if (A.at(i) == B.at(j))
//using the .at(i) will throw an exception if anything goes out of range of the container.
//Operator [] doesn't provide this safety.
{
idx.push_back(A.at(i));
i++;
j++;
}
else if(A.at(i) < B.at(j))
{
i++;
}
else if (A.at(i) > B.at(j))
{
j++;
}
}

return idx;
//you didn't actually return anything before

}

int main()
{
int myintsA[]={1,3,3,2,2,5,5,5,4};
int myintsB[]={0,9,8,6,7,3,3,4};

vector<int> myvectorA (myintsA, myintsA + sizeof(myintsA) / sizeof(int) );
vector<int> myvectorB (myintsB, myintsB + sizeof(myintsB) / sizeof(int) );


sort(myvectorA.begin(),myvectorA.end());
sort(myvectorB.begin(),myvectorB.end());

list<int> result = findDup(myvectorA, myvectorB);
for(list<int>::iterator iter = result.begin(); iter!=result.end();++iter)
{
cout<< *iter ;
//using cout is generally safer and more idiomatic c++
}
cout << endl;

return 0;
}

主要问题是最后发生的边缘情况。有几点需要注意:如果您使用 .at(i) 语法,您会抛出一个 std::out_of_range ,这会为您指出正确的方向来发现问题。此外,如果您使用 -Wall 进行编译,您会收到关于第一个函数未返回任何内容的警告。

关于c++ - 循环永无止境??~需要帮助。 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5131694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com