c++根据逗号截取string字符串不是使用字符串流 stringstream
时间: 2024-01-01 15:03:52 浏览: 114
你说得对,使用字符串流 stringstream 是更好的选择。字符串流 stringstream 可以很方便地对字符串进行分割,而且比手动解析更加安全可靠。以下是使用字符串流 stringstream 实现按逗号分割 string 的示例代码:
```c++
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string str = "hello,world,how,are,you";
vector<string> vec;
stringstream ss(str);
string token;
while (getline(ss, token, ',')) {
vec.push_back(token);
}
for (auto s : vec) {
cout << s << endl;
}
return 0;
}
```
输出:
```
hello
world
how
are
you
```
阅读全文
相关推荐
