Skip to content

Instantly share code, notes, and snippets.

@trentbrooks
Last active August 29, 2015 13:57
Show Gist options
  • Save trentbrooks/9357821 to your computer and use it in GitHub Desktop.
Save trentbrooks/9357821 to your computer and use it in GitHub Desktop.
stringstream extraction
string data = "hello there";
// standard stringstream extraction based on ' ' character
stringstream ss(data);
string extract1;
ss >> extract1; // hello
cout << extract1 << endl;
string extract2;
ss >> extract2; // there
cout << extract2 << endl;
// custom delim ':' extraction
data = "hello there:sir";
stringstream ssCustomDelim(data);
string extract3;
getline(ssCustomDelim, extract3, ':');
cout << extract3 << endl; // hello there
// inserting different types with ' ' delim, then extracting
stringstream ssVars;
ssVars << "hi"; // insert string
ssVars << " "; // must seperate vars with a space
ssVars << 0.9; // insert float
ssVars << " ";
ssVars << 777; // insert int
string extractString;
ssVars >> extractString; // hi
float extractFloat;
ssVars >> extractFloat; // 0.9
int extractInt;
ssVars >> extractInt; // 777
cout << extractString << ", " << extractFloat << ", " << extractInt << endl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment