4

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
  • 4,353
  • 8
  • 36
  • 45
  • possible duplicate of [How to split a string in C++?](http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c) – user7116 Dec 30 '11 at 20:15
  • 3
    I think this question has been asked a few times already: http://stackoverflow.com/questions/1894886/parsing-a-comma-delimited-stdstring http://stackoverflow.com/questions/4328685/input-line-by-line-from-an-input-file-and-tokenize-using-strtok-and-the-output http://stackoverflow.com/questions/536148/c-string-parsing-python-style http://stackoverflow.com/questions/1511029/c-tokenize-a-string-and-include-delimiters http://stackoverflow.com/questions/3162108/a-better-way-to-split-a-string-into-an-array-of-strings-in-c-c-using-whitespac – Jared Krumsie Dec 30 '11 at 20:23
  • Thank you, sixletter and Jared. Your threads provided me with a lot of good knowledge on this topic. Sorry I didn't do a thorough search in this forum before posting. Thanks again! – Qiang Xu Dec 30 '11 at 20:33

5 Answers5

4

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 "));
alex tingle
  • 6,920
  • 3
  • 25
  • 29
  • Thanks, but I intend to discard the use of stream if possible. – Qiang Xu Dec 30 '11 at 19:56
  • 1
    Thank 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? – Qiang Xu Dec 30 '11 at 20:13
  • Sorry for the ninja edit there. I mistook your question, at first. – alex tingle Dec 30 '11 at 20:26
  • By 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 Dec 30 '11 at 20:37
  • 1
    @Qiang : You tagged your own question `stream` but you're trying to avoid streams? That's slightly misleading. – ildjarn Dec 30 '11 at 21:43
3
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);
Sudeep
  • 53
  • 1
  • 9
2

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)
                 )
    ...
}
Martin York
  • 257,169
  • 86
  • 333
  • 562
0

Use ostringstream, maybe

istringstream(stringInput); // initialize with the string
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
  • Yep, 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 Dec 30 '11 at 19:51
0

In Visual C++ 11 you can use regex_token_iterator from TR1.

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) << ' ';
 }

}
Peter
  • 346
  • 5
  • 7
  • Thank you, Petro. Your split routine is quite similar to Andrew Koenig's in his "Accelerated C++". Both are nice functions. – Qiang Xu Dec 30 '11 at 21:24