官方说明: (1) istream&getline(istream&is,string&str,char delim); istream&getline(istream && is,string&str,char delim); (2) istream&getline(istream&is,string&str); istream&getline(istream && is,string&str); 从流中获取行字符串 从is中提取字符并将它们存储到str中,直到找到分隔字符delim(或换行符,(2)中默认为'\ n')。
is:
istream object from which characters are extracted.
str:
string object where the extracted line is stored.
The contents in the string before the call (if any) are discarded and replaced by the extracted line.
例如:
第一行输入一个n,代表接下来输入n行字符串(每行字符串可以包含空格)
3 aa aa bbb ccc
#include <iostream> #include <vector> using namespace std; int main() { vector<string> vec; int n; cin >> n; cin.get();//由于输入n之后回车,使用这句回车符号吃掉,否则下面的getline()获取的第一个字符串为'\n' while(n--) { string s; getline(cin, s); //默认为回车符,若以其他符号为分隔符,改为getline(cin, s, ','),例如逗号 vec.push_back(s); } cout<< "result: " <<endl; for(int i=0; i<vec.size(); ++i) { cout << vec.at(i) << endl; } system("pause"); return 0; }
若没有cin.getr()将 '\n' 吃掉,则会出现以下情况:
输入两次便不可在输入,输出结果中第一行为空(只有一个回车符号,所以显示为空)
