【C++】字符串

如何分割带有逗号的字符串(以逗号为分界)

1
2
(1) istream& getline(istream& is, string& str, char delim);
(2) istream& getline(istream& is, string& str);

从is开始读取字符,直到遇到分隔符delim(1)或者换行符(2),将读取的内容存入str。下一次调用,str的内容会被新分割的字段替换。

示例代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include <sstream>

vector<string> stu;
string ss;
cin >> ss;

stringstream sstr(ss);
string token;
int be=0;
while(getline(sstr, token, ','))
{
stu.push_back(token);
}
Author: iwannaeat
Link: https://iwannaeat.github.io/2020/08/14/【C++】字符串/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.