std::string 拆分
时间: 2024-01-12 15:04:02 浏览: 92
以下是使用C++拆分std::string的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
std::vector<std::string> splitString(const std::string& str, char delimiter) {
std::vector<std::string> result;
std::stringstream ss(str);
std::string token;
while (std::getline(ss, token, delimiter)) {
result.push_back(token);
}
return result;
}
int main() {
std::string str = "a;b;c;d;e;f";
char delimiter = ';';
std::vector<std::string> splitResult = splitString(str, delimiter);
for (const std::string& s : splitResult) {
std::cout << s << std::endl;
}
return 0;
}
```
运行上述代码,将会输出拆分后的结果:
```
a
b
c
d
e
f
```
阅读全文