w*****j 发帖数: 49 | 1 Hi, everyone. I've been trying to use cin to read a sentence I typed in into
a vector of string . But it seems like it doesn't work. It only read in the
first word. Does anybody get any suggestion or tell me how cin deals with
strings? | S**I 发帖数: 15689 | 2 getline(cin, str);
into
the
【在 w*****j 的大作中提到】 : Hi, everyone. I've been trying to use cin to read a sentence I typed in into : a vector of string . But it seems like it doesn't work. It only read in the : first word. Does anybody get any suggestion or tell me how cin deals with : strings?
| z****e 发帖数: 2024 | 3 #include
#include
ifstream inf("your_file_wants_to_be_read.txt");//source file
vector vs((istream_iterator(inf)),
istream_iterator());//read into string vector
for the second sentence, pay attention, do not be fooled by the "most vexing
parse of C++",
else you will only define a function rather than a vector. | z****e 发帖数: 2024 | 4 or if you really want to get screen command line input as source, and you
want " " as delimiter.
string s1;
vector vstr;
getline(cin,s1);//command line input
size_t p1=0;
size_t p2=0;
while( (p2=s1.find_first_of(" ",p2)) != string::npos){
cout<
vstr.push_back(s1.substr(p1, p2-p1));
p2=s1.find_first_not_of(" ",p2);
p1=p2;
}
vstr.push_back(s1.substr(p1,p2-p1));//read the last string |
|