if the token is separated by character like ':' or '|' how can you use sstream to tokenize it?

<< moderator edit: split thread from [thread=27724]here[/thread] >>

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   std::string token, text("Here:is:some:text");
   std::istringstream iss(text);
   while ( getline(iss, token, ':') )
   {
      std::cout << token << std::endl;
   }
   return 0;
}

/* my output
Here
is
some
text
*/

Pretty neat solution, thanks.
Here is a Tokenizer class from Codeproject, which can tokenize string separated by
different chars like space, coma, predefined char group etc.

//Tokenizer.h

#ifndef __TOKENIZER_H__
#define __TOKENIZER_H__

#include <functional>
#include <string>
#include <vector>
#include <algorithm>
#include <locale>

using namespace std;

//For the case the default is a space.
//This is the default predicate for the Tokenize() function.
class CIsSpace : public unary_function<char, bool>
{
public:
  bool operator()(char c) const;
};

inline bool CIsSpace::operator()(char c) const
{
  //isspace<char> returns true if c is a white-space character (0x09-0x0D or 0x20)
  //return isspace<char>(c);
  return isspace(c);
}

//For the case the separator is a comma
class CIsComma : public unary_function<char, bool>
{
public:
  bool operator()(char c) const;
};

inline bool CIsComma::operator()(char c) const
{
  return (',' == c);
}

//For the case the separator is a character from a set of characters given in a string
class CIsFromString : public unary_function<char, bool>
{
public:
  //Constructor specifying the separators
  CIsFromString::CIsFromString(string const& rostr) : m_ostr(rostr) {}
  bool operator()(char c) const;

private:
  string m_ostr;
};

inline bool CIsFromString::operator()(char c) const
{
  int iFind = m_ostr.find(c);
  if(iFind != string::npos)
    return true;
  else
    return false;
}

//String Tokenizer
template <class Pred=CIsSpace>
class CTokenizer
{
public:
  //The predicate should evaluate to true when applied to a separator.
  static void Tokenize(vector<string>& roResult, string const& rostr, Pred const& roPred=Pred());
};

//The predicate should evaluate to true when applied to a separator.
template <class Pred>
inline void CTokenizer<Pred>::Tokenize(vector<string>& roResult, string const& rostr, Pred const& roPred)
{
  //First clear the results vector
  roResult.clear();
  string::const_iterator it = rostr.begin();
  string::const_iterator itTokenEnd = rostr.begin();
  while(it != rostr.end())
  {
    //Eat seperators
    while(roPred(*it))
      it++;
    //Find next token
    itTokenEnd = find_if(it, rostr.end(), roPred);
    //Append token to result
    if(it < itTokenEnd)
      roResult.push_back(string(it, itTokenEnd));
    it = itTokenEnd;
  }
}
#endif //__TOKENIZER_H__

To use it:

vector<string> oResult;
      CTokenizer<CIsFromString>::Tokenize(oResult, path, CIsFromString(":"));
      for(int i=0; i<oResult.size(); i++)   cout << oResult[i] << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.