由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - how to read a sentence into a vector of string?
相关主题
istream_iterator问题C++读文本文件怎么判断换行?
读取数据求教问一个C++文件读取的问题
问一段C++ iostringstream的代码C++ Q 99-102 (转载)
如何把文件内容读到2D的vector里?请教一个C++的问题
关于文件读取的C++ 问题?C++ 在 windows 上 结果正确, 在 linux 上结果总是不一样,怎
天,如何能让程序转得快点?有包子。help on string parse
C++ Q13: InputC++ read matrix from txt file
输入输出流,stl,api精通各需要多长时间?C++ string类输入数据的问题
相关话题的讨论汇总
话题: string话题: vector话题: read话题: sentence话题: p2
进入Programming版参与讨论
1 (共1页)
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
1 (共1页)
进入Programming版参与讨论
相关主题
C++ string类输入数据的问题关于文件读取的C++ 问题?
C++ string to int Problem天,如何能让程序转得快点?有包子。
继续请教C++重载问题,>>C++ Q13: Input
能否对某个库进行操作符重载?输入输出流,stl,api精通各需要多长时间?
istream_iterator问题C++读文本文件怎么判断换行?
读取数据求教问一个C++文件读取的问题
问一段C++ iostringstream的代码C++ Q 99-102 (转载)
如何把文件内容读到2D的vector里?请教一个C++的问题
相关话题的讨论汇总
话题: string话题: vector话题: read话题: sentence话题: p2