[英]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.