Last active
August 29, 2015 13:57
-
-
Save trentbrooks/9357821 to your computer and use it in GitHub Desktop.
stringstream extraction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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