3/11/2014

C,C++, string split example source code (stringstream, getline functions)


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

using namespace std;

vector< string> str_split(const string &s, char delim) 
{
    vector< string> elems;
    stringstream ss(s);
    string item;
    
    while (getline(ss, item, delim)) {
        elems.push_back(item);
    }
 
    return elems;
}



int main()
{

 vector< string> str = str_split( "123,456", ',');
 for(int i=0; i< str.size(); ++i)
 {
  printf("%s\n", str[i].c_str() );
 }

}


No comments:

Post a Comment