首页 > 代码库 > sstream

sstream

sstream用法 

 1 #include<iostream>
 2 #include<sstream>
 3 #include<string>
 4 using namespace std;
 5 
 6 int main(){
 7     /* istringstream 用法 */
 8     istringstream instr;
 9     string str = "westerDB 1024";
10     instr.str(str);    //只能存string值
11     //instr.str(to_string(123));
12     //cout<<instr.str()<<instr.str();    //并不会影响instr的内部内容
13 
14     string hdisk;
15     instr>>hdisk;
16     //getline(instr, hdisk);
17     int cap;
18     instr>>cap;
19 
20     //cout<<instr.str();
21     cout<<hdisk<<endl<<cap;
22     return 0;
23 }
24 
25 /* ostringstream用法 */
26 int testOutStr(){
27     ostringstream outstr;
28     /*istringstream instr;
29     string str = "westerDB 1024";
30     instr.str(str);*/
31 
32     string hdisk;
33     cout<<"what‘s the name of your hard disk?";
34     getline(cin, hdisk);
35 
36     int cap;
37     cout<<"what‘s its capacity in GB?";
38     cin>>cap;
39 
40     outstr<< "The hard disk "<<hdisk<<" has a capacity of "
41         <<cap<<" gigabytes.\n";
42 
43     string result = outstr.str();
44     cout<<result;
45     return 0;
46 }

 

sstream