저는 코딩에 익숙하지 않고 누군가 나를 도울 수 있기를 바랬습니다. 나는 공백으로 구분 된 정수의 라인을 읽으려고 노력하고 있으며 (궁극적으로 연결된리스트로) 벡터를 파싱한다.문자열을 int로 구문 분석
내가 int의 벡터를 가지면, STL 벡터의 반복자가 있지만, STL이 아닌 링크리스트의 노드를 반복 할 수 있습니까?
#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
을 종료합니다 : 그냥 왜
#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;
}
이 이후의 자신을 생각 나게 :'표준 : istream_iterator, 마지막으로 (이제 stringstream) 첫째; std :: vector parsedString (첫 번째, 마지막);'문자열을 컨테이너로 분할하기위한 것입니다. (실제로는 그렇게 생각하지 않았습니다). 이 사이트의 인기있는 질문에는 다른 방법이 있습니다. –
chris
왜'int a; while (cin >> a) {// do something}' – gongzhitaao
당신이 직접 연결된 목록을 구현하고 싶습니까? – gongzhitaao