Use of stringstream object
Today, I made a case-sensitive question about String Conversion. After that, I suddenly wanted to see how others wrote it, as a result, I found a method of using the C ++ stream described in the title: it is very convenient, but even though I have completed the C ++ course, I am actually a (wu) point (li) also (to) Not (cao) knows (!) Oh, it's really embarrassing !!, It is necessary to go back and study it!
Click here
Use of 1stringstream object
# Include
# Include
Using namespace std; int main () {string line, word; while (getline (cin, line) {stringstream stream (line); cout <
<
> Word) {cout <
<
About
getline
And Related Input Usage Details can refer to the translation of another: http://blog.csdn.net/u013050857/article/details/43304025
Input: shanghai no1 school 1989
Output: shanghi no1 school 1989.
Shanghai
No1
School
1989
2stringstream provides conversion and formatting
# Include
# Includewww.bkjia.com
Using namespace std; int main () {int val1 = 512, val2 = 1024; stringstream ss; ss <
> Dump> a> dump> B; cout <
Output: val1: 512
Val2: 1024
512 1024
Part 1 in bold: reads int type into ss and changes it to string type
The second part of the black text: extract and save as int type. Of course, if a and B are declared to be of the string type, the two literal constants are saved as strings.
3 other notes
# Include
# Include
# Include
Using namespace std; int main () {stringstream ss; string s; ss <
> S; cout <
Output:
Size of stream = 19 s: shanghaisize of stream = 0
, Exercise questions HDU 2564
Code ;'
// General statement # include
# Include
# Include
# Include
# Include using namespace std; char str [1001]; int main () {int n; int I; cin> n // enter the number of tests; getchar (); while (n --) {gets (str); // input phrase; for (I = 0; I
= 'A' & str [I] <= 'Z') str [I]-= 32; for (I = 0; I
# Include
# Include
# Include
# Include
Using namespace std; char str [1001]; int main () {int n; cin> n; getchar (); while (n --) {cin. getline (str, 1000); stringstream str (str); char a [20]; while (str> a) cout <(char) toupper (a [0]); // toupper function: converts lowercase letters to uppercase letters, and tolower function-: converts uppercase letters to lowercase letters cout <
<
<