how to parse a string in C++

Hi all,

I want to parse a std::string, the delimitor is '\n'. For example,

string s = "hi all \n how r u \n welcome every one \n";

i want this to be in a list or vector.


Can anyone help me how to do this?
Use a stringstring:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <sstream>
#include <string>
#include <vector>
using namespace std;

...

vector <string> lines;
string s = "hi all \n how r u \n welcome every one \n";
{
  istringstream ss( s );
  string line;
  while (getline( ss, line ))
    lines.push_back( line );
}

Hope this helps.
Use a stringstring

hmm didn't you mean stringstream?
Topic archived. No new replies allowed.