使用C ++ 11分割string

什么是最简单的方法来分裂一个string使用C + + 11?

我已经看到了这篇文章所使用的方法,但是我觉得应该使用新的标准来减less冗长的方法。

编辑:我想有一个vector<string>作为结果,并能够在一个单一的字符分隔。

std::regex_token_iterator执行基于正则expression式的通用标记。 对单个字符进行简单的分割可能会也可能不会矫枉过正,但是它的工作原理并不太冗长:

 std::vector<std::string> split(const string& input, const string& regex) { // passing -1 as the submatch index parameter performs splitting std::regex re(regex); std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last; return {first, last}; } 

这是一个(也许不太详细)的方式来拆分string(根据您提到的职位 )。

 #include <string> #include <sstream> #include <vector> std::vector<std::string> split(const std::string &s, char delim) { std::stringstream ss(s); std::string item; std::vector<std::string> elems; while (std::getline(ss, item, delim)) { elems.push_back(item); // elems.push_back(std::move(item)); // if C++11 (based on comment from @mchiasson) } return elems; } 

我不知道这是不是很冗长,但对于那些在javascript这样的dynamic语言中更经验丰富的人来说可能更容易些。 它使用的唯一C ++ 11function是lambdas。

 #include <algorithm> #include <string> #include <cctype> #include <iostream> #include <vector> int main() { using namespace std; string s = "hello how are you won't you tell me your name"; vector<string> tokens; string token; for_each(s.begin(), s.end(), [&](char c) { if (!isspace(c)) token += c; else { if (token.length()) tokens.push_back(token); token.clear(); } }); if (token.length()) tokens.push_back(token); return 0; } 

我的select是boost::tokenizer但我没有任何繁重的任务,并testing了大量的数据。 使用lambda修改的boost文件示例:

 #include <iostream> #include <boost/tokenizer.hpp> #include <string> #include <vector> int main() { using namespace std; using namespace boost; string s = "This is, a test"; vector<string> v; tokenizer<> tok(s); for_each (tok.begin(), tok.end(), [&v](const string & s) { v.push_back(s); } ); // result 4 items: 1)This 2)is 3)a 4)test return 0; } 
 #include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; vector<string> split(const string& str, int delimiter(int) = ::isspace){ vector<string> result; auto e=str.end(); auto i=str.begin(); while(i!=e){ i=find_if_not(i,e, delimiter); if(i==e) break; auto j=find_if(i,e, delimiter); result.push_back(string(i,j)); i=j; } return result; } int main(){ string line; getline(cin,line); vector<string> result = split(line); for(auto s: result){ cout<<s<<endl; } } 

以下是一个分割string并使用boost提取元素填充vector的示例。

 #include <boost/algorithm/string.hpp> std::string my_input("A,B,EE"); std::vector<std::string> results; boost::algorithm::split(results, my_input, is_any_of(",")); assert(results[0] == "A"); assert(results[1] == "B"); assert(results[2] == "EE"); 

这是我的答案。 详尽,可读和高效。

 std::vector<std::string> tokenize(const std::string& s, char c) { auto end = s.cend(); auto start = end; std::vector<std::string> v; for( auto it = s.cbegin(); it != end; ++it ) { if( *it != c ) { if( start == end ) start = it; continue; } if( start != end ) { v.emplace_back(start, it); start = end; } } if( start != end ) v.emplace_back(start, end); return v; } 

另一个正则expression式的解决scheme,其他答案的启发,但希望更短,更容易阅读:

 std::string s{"String to split here, and here, and here,..."}; std::regex regex{R"([\s,]+)"}; // split on space and comma std::sregex_token_iterator it{s.begin(), s.end(), regex, -1}; std::vector<std::string> words{it, {}};