red floyd <no*****@here.dudewrote in message...
JackC wrote:
Hi,
How do i use stringstreams getline function to extract lines from an
existing string?
Say i have: string strlist = "line1\r\nLine2\r\nLine3\r\n";
I want to extract each line out into a vector array of strings, but i
cant get stringstream working :(
This is the sort of code i have tried:
// istream& getline (char* s, streamsize n )
stringstream MyStream;
MyStream<<strlist;
string extracted_line = MyStream.getline(mystring, 0);
#include <string>
#include <sstream>
#include <istream>
#include <vector>
int main(){
std::string strlist("line1\r\nLine2\r\nLine3\r\n");
std::istringstream MyStream(strlist);
std::vector<std::stringv;
// std::string s;
// while (std::getline(MyStream, s))
// v.push_back(s);
// ref: A. Koenig/W. Brown
for( std::string line; std::getline( MyStream, line ); ){
v.push_back(line);
} // for(line)
return 0;
}
Why? Because you can! <GHi red.
To OP (from a post by Gavin Dean):
std::getline allows you to choose any delimiter character you want. The
default is newline but you can change that. This shows how it works.
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main(){
string input("abc\tdef\nghi");
istringstream iss(input);
string read_to_newline;
getline(iss, read_to_newline);
iss.str(input);
string read_to_tab;
getline(iss, read_to_tab, '\t');
cout << read_to_newline << "\n";
cout << read_to_tab << "\n";
}
--
Bob R
POVrookie