C++ code:string stream(string流)学习大全
問題一:
如果有一個(gè)文件aaa.txt,有若干行,不知道每行中含有幾個(gè)整數(shù),要編程輸出每行的整數(shù)之和,該如何實(shí)現(xiàn)?由于cin>>不能辨別空格與回車的差異,因此只能用getline的方式逐行讀入數(shù)據(jù)到string變量中,但在string變量中分離若干個(gè)整數(shù)還是稍顯吃力。一個(gè)好的方法是用string流:
#include<iostream> #include<sstream> #include<fstream> using namespace std; int main() {ifstream in("aaa.txt");for (string s; getline(in, s);){int a, sum = 0;for (istringstream sin(s); sin >> a; sum += a);cout << sum << endl;}cin.get();return 0; }講道理,該程序編得有些放肆。本該將istringstream sin(s)單獨(dú)占一行,結(jié)果非但不然,還將sum+=a都縮到循環(huán)結(jié)構(gòu)描述的步長部分中去了。這樣一來,循環(huán)體便為空了,于是,for循環(huán)的描述部分后面加上分號(hào)便自成獨(dú)立的語句,但它確實(shí)能夠完成累計(jì)工作。作為單獨(dú)的循環(huán),最后的“;”還是不能忘記的!!因?yàn)槌绦蛐?#xff0c;所以可讀性還不到受傷害的地步,請(qǐng)讀者來見識(shí)一下著這種風(fēng)格。
istringstream是輸入string流,它在sstream頭文件中說明。該語句類似文件流操作,只不過創(chuàng)建sin流時(shí),其參數(shù)為string對(duì)象。它是將string的實(shí)體看作是一個(gè)輸入流,因而,sin>>a即是從string流中輸入整數(shù)到a中,輸啊輸,一直輸?shù)絪tring中的最后一個(gè)整數(shù)!
? ? ? ? string流很有用,有時(shí)候要將內(nèi)容逐個(gè)輸出到string中,最后才根據(jù)計(jì)算結(jié)果來編排輸出格式。這時(shí)候,用string流就很管用。
拓展:加入一個(gè)文件里面有一些實(shí)數(shù),那么按照上述方法求和。
#include <string> #include <iostream> #include <fstream> #include <sstream> using namespace std; int main() {ifstream in("123.txt");string i_read;double d_number,sum = 0;while(getline(in,i_read)){for(istringstream s(i_read);s >> d_number;sum += d_number){cout << "double number is: " << d_number <<endl;cout << "sum is: " << sum << endl << endl;}}return 0; } 以下是文件 123.txt 中的實(shí)數(shù) 1.23 2.21 3.123 1.2 1 2.2 .4 1.23 1 2 3 1.2 .1 .23問題二:可以用于分割被空格、制表符等符號(hào)分割的字符串
#include<iostream> #include<sstream> //istringstream 必須包含這個(gè)頭文件 #include<string> using namespace std; int main(){ string str="i am a boy"; istringstream is(str); string s; while(is>>s) { cout<<s<<endl; } }輸出結(jié)果:
i am a boy二、
//Example:stringstream、istringstream、ostringstream的構(gòu)造函數(shù)和用法
#include <iostream> #include <sstream> int main() {// default constructor (input/output stream)std::stringstream buf1;buf1 << 7;int n = 0;buf1 >> n;std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';// input streamstd::istringstream inbuf("-10");inbuf >> n;std::cout << "n = " << n << '\n';// output stream in append mode (C++11)std::ostringstream buf2("test", std::ios_base::ate);buf2 << '1';std::cout << buf2.str() << '\n'; } 輸出結(jié)果:buf1 = 7 n = 7 n = -10 test1三、
//Example:stringstream的.str()方法
#include <sstream> #include <iostream> int main() {int n;std::istringstream in; // could also use in("1 2")in.str("1 2");in >> n;std::cout << "after reading the first int from \"1 2\", the int is "<< n << ", str() = \"" << in.str() << "\"\n";std::ostringstream out("1 2");out << 3;std::cout << "after writing the int '3' to output stream \"1 2\""<< ", str() = \"" << out.str() << "\"\n";std::ostringstream ate("1 2", std::ios_base::ate);ate << 3;std::cout << "after writing the int '3' to append stream \"1 2\""<< ", str() = \"" << ate.str() << "\"\n"; }輸出結(jié)果:
after reading the first int from "1 2", the int is 1, str() = "1 2" after writing the int '3' to output stream "1 2", str() = "3 2" after writing the int '3' to append stream "1 2", str() = "1 23"注意事項(xiàng):
由于stringstream構(gòu)造函數(shù)會(huì)特別消耗內(nèi)存,似乎不打算主動(dòng)釋放內(nèi)存(或許是為了提高效率),但如果你要在程序中用同一個(gè)流,反復(fù)讀寫大量的數(shù)據(jù),將會(huì)造成大量的內(nèi)存消耗,因些這時(shí)候,需要適時(shí)地清除一下緩沖 (用 stream.str("") )。參考鏈接:
istringstream、ostringstream、stringstream 類介紹 .
C++中的 istringstream 的用法
?
?
總結(jié)
以上是生活随笔為你收集整理的C++ code:string stream(string流)学习大全的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos 安装rar 和 unrar
- 下一篇: C++ int转string的几种方法比