[英]loop never end ??~ need help. C++
问题:
给定这样的输入:
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;
}
但是我的程序似乎错了。 需要帮忙! 谢谢!
findDup
函数存在边界条件错误:如果i
或j
一个在末尾,而另一个不在末尾怎么办? 您的循环将继续,访问超出向量的范围。 对于您正在执行的操作,您应该只需要将循环条件更改为(i != A.size()) && (j != B.size())
(将||
更改为&&
)即可。
另一个问题是%c
格式用于字符。 %d
是int
s的,如果您使用%c
则终端上会有奇怪的结果。 您还应该在输出列表的开头打印(假设您要在问题中显示的格式)左括号,在输出数字之间用逗号分隔,并在末尾使用右括号和换行符。
@Tim的答案也是正确的; 您的代码中也有错字。
else if(A[i] < B[i])
我想你的意思是:
else if(A[i] < B[j]) // B uses j, not i
这可能是您无限循环的原因。
编辑:正如耶利米指出的那样,你的情况一团糟。 正常做法看起来像这样:
while(i < A.size() && j < B.size()) {
这是先前发布的代码的有效版本:
#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
编译,则会警告您第一个函数不返回任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.