-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTRINGstream_Explainer.cpp
More file actions
49 lines (37 loc) · 1.27 KB
/
Copy pathSTRINGstream_Explainer.cpp
File metadata and controls
49 lines (37 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <sstream>
using namespace std;
//use stringstream to convert int to string.
/*int main() {
int num = 54;
stringstream ss;
ss << num;
:string str = ss.str();
std::cout << "String. " << str << std::endl;
}
*/
//use string stream to split a string.
/*
int main () {
string data = "Apple banana mango";
stringstream ss(data);
string word;
while (ss >> word) {
cout << word << endl;
}
}
*/
//Multiple types from one string.
int main() {
string input = "25 Arga";
stringstream ss(input); //This makes a stringstream object with the command and ss.
int age; //Defining age variable as int.
string name; //Defining name variable as String.
ss >> age >> name; //By using the ss command it takes 1st word as int and 2nd word as String.
//Therefore we get different types from 1 string.
cout << "age: " << age << ", Name: " << name << endl;
}
//Some use cases
//Parsing user input, Debugging, Converting string to int (vice versa)
// Alternatives Manual Parsing, cout, printf, stoi, to_string
//Why use it? => It gives more control, is cleaner, useful for building formatted strings.