我对编码很陌生,我希望有人能帮助我?我试图在一行空格分隔的整数中读取,并将它们解析为(最终进入链表)向量。
所以一旦我有一个int型向量,有STL向量的迭代器,但我怎么能迭代通过链表中的节点不是STL?
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
cout << "Enter some integers, space delimited:n";
string someString;
getline(cin, someString);
istringstream stringStream( someString );
vector<string> parsedString;
char splitToken = ' ';
//read throguh the stream
while(!stringstream.eof()){
string subString;
getline( stringStream, subString, splitToken);
if(subString != ""){
parsedString.push_back(subString);
}
}
return EXIT_SUCCESS;
}
stringstream
可以自动处理如下分隔符:
cout << "Enter some integers, space delimited:n";
string someString;
getline(cin, someString);
istringstream stringStream( someString );
vector<int> integers;
int n;
while (stringStream >> n)
integers.push_back(n);
既然是空格分隔符,为什么不直接:
#include <iostream>
using namespace std;
int main() {
int a;
vector<int> v;
while (cin >> a) {
v.push_back(a);
}
for (int i = 0; i < v.size(); ++i) {
int b = v[i];
}
return 0;
}
和BTW, ctrl-D
或非整数输入,如char
,将终止此while
。