程序在逗号之间找到整数,如“2,33,5”-> 2 33 5。问题是如果我把例如“0,12,4”这样的字符串,它为什么工作。stringstream 不应该将 0 放入 tmp 中,所以循环就像开始时的 while(0) 一样吗?
vector<int> parseInts(string str) {
stringstream ss(str); //getting string
vector<int> result;
char ch;
int tmp;
while(ss >> tmp) { //while(IS IT INTEGER ALREADY OR NOT?)
result.push_back(tmp);
ss >> ch;
}
return result;
Run Code Online (Sandbox Code Playgroud) 我有六个组件数组,我想确保如果用户正在初始化组件的值,他/她将不会给出2个或更多相同的数字.
while (tab[i] == tab[i - 1] || tab[i] == tab[i - 2] || tab[i] == tab[i - 3] || tab[i] == tab[i - 4] || tab[i] == tab[i - 5])
{
cout << "Liczby nie moga sie powtarzac! Sproboj jeszcze raz: ";
cin >> tab[i];
}
Run Code Online (Sandbox Code Playgroud)
我试着这样做:
while (for (int m = 1; m < 6; ++m)
{
tab[i] == tab[i - m];
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用:/