[英]loop never end ??~ need help. C++
Quesition: 问题:
Given input like this: 给定这样的输入:
int myintsA[]={1,3,3,2,2,5,5,5,4};
int myintsB[]={0,9,8,6,7,3,3,4};
Find same elements, out put it. 找到相同的元素,把它放出来。 When they appear both 2 times. 当它们同时出现2次时。 out put it as 2 times. 拿出来作为2倍。
For example: 例如:
Output: {3,3,4} 输出:{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;
}
But my program seems wrong. 但是我的程序似乎错了。 need help! 需要帮忙! THank you! 谢谢!
The findDup
function has a boundary condition error: what if one of i
or j
is at the end, but the other isn't? findDup
函数存在边界条件错误:如果i
或j
一个在末尾,而另一个不在末尾怎么办? Your loop will continue, accessing beyond the end of the vector. 您的循环将继续,访问超出向量的范围。 For what you are doing, you should just be able to change the loop condition to (i != A.size()) && (j != B.size())
(changing ||
to &&
). 对于您正在执行的操作,您应该只需要将循环条件更改为(i != A.size()) && (j != B.size())
(将||
更改为&&
)即可。
Another issue is that the %c
format is for characters; 另一个问题是%c
格式用于字符。 %d
is for int
s, and there will be strange results on your terminal if you use %c
. %d
是int
s的,如果您使用%c
则终端上会有奇怪的结果。 You should also print (assuming you want the format you show in the question) a left brace at the beginning of the output list, commas between output numbers, and a right-brace and newline at the end. 您还应该在输出列表的开头打印(假设您要在问题中显示的格式)左括号,在输出数字之间用逗号分隔,并在末尾使用右括号和换行符。
@Tim's answer is also correct; @Tim的答案也是正确的; you have that typo in your code as well. 您的代码中也有错字。
else if(A[i] < B[i])
I assume you meant: 我想你的意思是:
else if(A[i] < B[j]) // B uses j, not i
that could be the cause of your infinite loop. 这可能是您无限循环的原因。
Edit: And as Jeremiah points out, your while condition is messed up. 编辑:正如耶利米指出的那样,你的情况一团糟。 Normal practice looks more like this: 正常做法看起来像这样:
while(i < A.size() && j < B.size()) {
Here's a working version of the code posted from earlier: 这是先前发布的代码的有效版本:
#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;
}
The main issue is the edge case that happens at the end. 主要问题是最后发生的边缘情况。 A few things to note: If you used the .at(i)
syntax you would have got a std::out_of_range thrown which would have pointed you in the right direction to finding the problem. 需要注意的几件事:如果您使用.at(i)
语法,则会抛出std :: out_of_range,这将为您指出寻找问题的正确方向。 Also if you compiled with -Wall
you would have been warned about the first function not returning anything. 同样,如果使用-Wall
编译,则会警告您第一个函数不返回任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.