string分割字符串
时间: 2023-09-30 20:11:48 浏览: 93
Arduino 分割字符串库,strFenGe.rar
在C++中,可以使用stringstream和getline函数来分割字符串。
示例代码:
```c++
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main() {
std::string str = "hello world,how are you?";
std::vector<std::string> result;
std::stringstream ss(str);
std::string token;
while (getline(ss, token, ',')) {
result.push_back(token);
}
for (const auto& s : result) {
std::cout << s << std::endl;
}
return 0;
}
```
该代码将字符串"hello world,how are you?"按照逗号分割,并将结果存储在vector容器中。运行结果为:
```
hello world
how are you?
```
阅读全文