有点我的代码如下所示:
static int myfunc(const string& stringInput)
{
string word;
stringstream ss;
ss << stringInput;
while(ss >> word)
{
++counters[word];
}
...
}
Run Code Online (Sandbox Code Playgroud)
这里的目的是获取一个输入字符串(由空格''分隔)到字符串变量中word,但这里的代码似乎有很多开销 - 将输入字符串转换为字符串流并从字符串流读取到目标字符串.
是否有更优雅的方式来实现相同的目的?
你问的是如何拆分字符串.Boost有一个有用的实用程序boost :: split()
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
这是一个将结果单词放入向量的示例:
#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("\t "));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29321 次 |
| 最近记录: |