如何在 C++ 中使用 boost::algorithm::split() 分割字符串 - 最小示例
split_example.cpp
#include <boost/algorithm/string/split.hpp> // boost::algorithm::split
#include <boost/algorithm/string/classification.hpp> // boost::is_any_of
#include <string>
#include <iostream>
#include <vector>
int main() {
std::string mystr = "foo-bar-x";
// 按 "-" 分割
std::vector<std::string> splitResult;
boost::algorithm::split(splitResult, mystr, boost::is_any_of("-"));
// 打印结果,每行一个
// 打印:
// foo
// bar
// x
for(const std::string& s : splitResult) {
std::cout << s << std::endl;
}
}If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow