C++ 如何在C++中将空格分隔的字符串拆分为多个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8683302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a space separated string into multiple strings in C++?
提问by Qiang Xu
Somewhat my code looks like below:
有点我的代码如下所示:
static int myfunc(const string& stringInput)
{
string word;
stringstream ss;
ss << stringInput;
while(ss >> word)
{
++counters[word];
}
...
}
The purpose here is to get an input string (separated by white space ' ') into the string variable word
, but the code here seems to have a lot of overhead -- convert the input string to a string stream and read from the string stream into the target string.
这里的目的是获取一个输入字符串(用空格''分隔)到字符串变量中word
,但是这里的代码好像有很大的开销——把输入字符串转换成字符串流,再从字符串流中读入目标字符串。
Is there a more elegant way to accomplish the same purpose?
有没有更优雅的方式来实现同样的目的?
采纳答案by alex tingle
You are asking how to split a string. Boost has a helpful utility boost::split()
您问的是如何拆分字符串。Boost 有一个有用的工具 boost::split()
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
http://www.boost.org/doc/libs/1_48_0/doc/html/string_algo/usage.html#id3115768
Here's an example that puts the resulting words into a vector:
这是一个将结果词放入向量的示例:
#include <boost/algorithm/string.hpp>
std::vector<std::string> strs;
boost::split(strs, "string to split", boost::is_any_of("\t "));
回答by Sudeep
Code in c++
#include<sstream>
#include<vector>
using namespace std;
string diskNames="vbbc anmnsa mansdmns";
string temp;
vector <string> cds;
stringstream s (diskNames);
while(s>> temp)
cds.push_back(temp);
回答by Martin York
Use stream iterators and a standard function:
使用流迭代器和标准函数:
static int myfunc(std::string const& stringInput)
{
std::stringstream ss(stringInput);
std::for_each(std::istream_iterator<std::string>(ss),
std::istream_iterator<std::string>(),
[&counters](std::string const& word) { ++counters[word];}
)
...
}
If you don't have lambda then:
如果您没有 lambda,则:
struct Helper
{
void operator()(std::string const& word) const {++counters[word];}
Helper(CounterType& c) : counters(c) {}
CounterType& counters;
};
static int myfunc(std::string const& stringInput)
{
std::stringstream ss(stringInput);
std::for_each(std::istream_iterator<std::string>(ss),
std::istream_iterator<std::string>(),
Helper(counters)
)
...
}
回答by Peter
In Visual C++ 11 you can use regex_token_iterator from TR1.
在 Visual C++ 11 中,您可以使用 TR1 中的 regex_token_iterator。
sregex_token_iterator::regex_type white_space_separators("[[:space:]]+",regex_constants::optimize);
for(sregex_token_iterator i(s.begin(),s.()end,white_space_separators,-1),end; i!=end; i++)
{
cout << *i << endl;
// or use i.start, i.end which is faster access
}
If you concerned about performance (and overheads like string copying), you can write your own routine:
如果您担心性能(以及字符串复制等开销),您可以编写自己的例程:
#include <ctype.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string s = "Text for tokenization ";
const char *start = s.c_str();
const char *end = start + s.size();
const char *token = start;
while (start!=end)
{
if(isspace(*start))
{
if (token < start)
{
// Instead of constructing string, you can
// just use [token,start] part of the input buffer
cout << string(token,start) << ' ';
}
start++;
token = start;
}
else
{
start++;
}
}
if (token < start)
{
cout << string(token,start) << ' ';
}
}
回答by Aaron McDaid
Use ostringstream, maybe
使用 ostringstream,也许
istringstream(stringInput); // initialize with the string