How to split a space separated string into multiple strings in C++?
31,504
Solution 1
You are asking how to split a string. Boost has a helpful utility boost::split()
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 "));
Solution 2
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);
Solution 3
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:
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)
)
...
}
Author by
Qiang Xu
Updated on July 09, 2022Comments
-
Qiang Xu almost 2 years
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.Is there a more elegant way to accomplish the same purpose?
-
Qiang Xu over 12 yearsYep, this saves a little bit. But the basic idea is still the same: construct a stringstream, and read from this stream. Could we not use the streamstream at all? And still read the space separated string into
word
? -
Qiang Xu over 12 yearsThanks, but I intend to discard the use of stream if possible.
-
Qiang Xu over 12 yearsThank you, alex. Yeah, this is what I want. But I am not sure whether my g++ compiler comes with boost library, how to check the existence of boost? Btw, any additional -l command to use in compiling the source code? If there is no boost available, is there a similar utility function in STL?
-
alex tingle over 12 yearsSorry for the ninja edit there. I mistook your question, at first.
-
Qiang Xu over 12 yearsBy the way, alex. Just find another split()'s implementation in the classical "Accelerated C++" by Andrew Koenig. The implementation is provided in Section 5.6 (Taking strings apart) of Chapter 5.
-
Qiang Xu over 12 yearsThank you, Loki. Your code looks awesome. Still, I prefer a way not to involve stream. :-)
-
Qiang Xu over 12 yearsThank you, Petro. Your split routine is quite similar to Andrew Koenig's in his "Accelerated C++". Both are nice functions.
-
ildjarn over 12 years@Qiang : You tagged your own question
stream
but you're trying to avoid streams? That's slightly misleading.