問題一:
如果有一個文件aaa.txt,有若干行,不知道每行中含有幾個整數,要編程輸出每行的整數之和,該如何實現?由於cin>>不能辨別空格與回車的差異,因此只能用getline的方式逐行讀入數據到string變量中,但在string變量中分離若干個整數還是稍顯吃力。一個好的方法是用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)單獨佔一行,結果非但不然,還將sum+=a都縮到循環結構描述的步長部分中去了。這樣一來,循環體便爲空了,於是,for循環的描述部分後面加上分號便自成獨立的語句,但它確實能夠完成累計工作。作爲單獨的循環,最後的“;”還是不能忘記的!!因爲程序小,所以可讀性還不到受傷害的地步,請讀者來見識一下着這種風格。
istringstream是輸入string流,它在sstream頭文件中說明。該語句類似文件流操作,只不過創建sin流時,其參數爲string對象。它是將string的實體看作是一個輸入流,因而,sin>>a即是從string流中輸入整數到a中,輸啊輸,一直輸到string中的最後一個整數!
string流很有用,有時候要將內容逐個輸出到string中,最後才根據計算結果來編排輸出格式。這時候,用string流就很管用。
拓展:加入一個文件裏面有一些實數,那麼按照上述方法求和。
#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 中的實數
1.23 2.21 3.123 1.2
1 2.2 .4 1.23
1 2 3
1.2 .1 .23
問題二:可以用於分割被空格、製表符等符號分割的字符串
#include<iostream>
#include<sstream> //istringstream 必須包含這個頭文件
#include<string>
using namespace std;
int main(){
string str="i am a boy";
istringstream is(str);
string s;
while(is>>s) {
cout<<s<<endl;
}
}
輸出結果:
i
am
a
boy
二、
//Example:stringstream、istringstream、ostringstream的構造函數和用法
#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 stream
std::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';
}
輸出結果:
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";
}
輸出結果:
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"
注意事項:
由於stringstream構造函數會特別消耗內存,似乎不打算主動釋放內存(或許是爲了提高效率),但如果你要在程序中用同一個流,反覆讀寫大量的數據,將會造成大量的內存消耗,因些這時候,需要適時地清除一下緩衝 (用 stream.str("") )。
參考鏈接:
istringstream、ostringstream、stringstream 類介紹 .