一、stringstream对象的使用
1、sstream头文件定义了三种类型的字符串流:
类型
|
说明
|
istringstream | 从istream派生而来,提供读string的功能; |
ostringstream
|
从ostream派生而来,提供写string的功能;
|
stringstream
|
从iostream派生而来,提供读写string的功能。
|
2、与iostream类型相比,stringstream增加了如下几个操作:
stringstream增加的操作 |
说明
|
stringstream strm;
|
创建自由的
stringstream对象;
|
stringstream strm(s);
|
创建存储s副本的
stringstream对象,其中s是string类型的对象;
|
strm.str(); | 返回strm中存储的string类型对象; |
strm.str(s);
|
将string类型的s复制给strm,返回void。
|
3、应用:逐行读取并逐个处理每行中的单词:
string line, word;
while (getline(cin, line))
{
istringstream stream(line);
while (stream >> word)
{
//...
}
}
4、应用:数值型数据转换为字符串型数据
int val1 = 512, val2 = 1024;
ostringstream format_message;
format_message << "val1: " << val1 << "\n"
<< "val2: " << val2 << "\n";
5、应用:字符串型数据转换为数值型数据
基于上面的处理:
istringstream input_istring(format_message.str());
string dump;
input_istring >> dump >> val1 >> dump >> val2;
cout << val1 << " " << val2 << endl;
注:使用输入操作符读取string时,空白符会被忽略。
习题8.15:读入数据至istringstream对象并输出
#include <iostream>
#include <sstream>
#include <stdexcept>
using namespace std;
istream& read_stream(istream& in)
{
int ival;
while (in >> ival, !in.eof())
{
if (in.bad())
{
throw runtime_error("IO stream corrupted!");
}
if (in.fail())
{
cout << "Read fail, try again." << endl;
in.clear();
in.ignore(2, ' ');
continue;
}
// ival is ok, process it
cout << ival << " ";
}
cout << endl;
in.clear();
return in;
}
int main()
{
string line;
cout << "Enter a line of text: " << endl;
getline(cin, line);
line += " ";
istringstream isstr(line);
read_stream(isstr);
return 0;
}
[chapter8]$ ./a.out
Enter a line of text:
123 456 789
123 456 789
习题8.16:将文件存储到vector<string>后按单词从容器中读取
/// 1. 读取文件并按行存储到vector<string>当中
/// 2. 使用istringstream从vector<string>中读取行中的每个单词
#include <iostream>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
int file_to_vector(const string &filename, vector<string> &svec)
{
ifstream infile(filename.c_str());
if (!infile)
{
cout << "Error: open file(" << filename << ") fail." << endl;
return 1;
}
string s;
while (getline(infile, s))
{
svec.push_back(s);
}
infile.close();
if (infile.eof())
{
cout << "OK: read to eof." << endl;
return 0;
}
if (infile.bad())
{
cout << "Error: system fail." << endl;
return 2;
}
if (infile.fail())
{
cout << "Error: read fail." << endl;
return 3;
}
return 0;
}
int main()
{
string filename;
cout << "Enter filename: " << endl;
cin >> filename;
vector<string> svec;
if (0 != file_to_vector(filename.c_str(), svec))
{
cout << "Error: file_to_vector() return fail." << endl;
return -1;
}
cout << "vector's size: " << svec.size() << endl;
cout << "vector:" << endl;
for (vector<string>::iterator it = svec.begin(); it != svec.end(); ++it)
{
cout << *it << endl;
}
string word;
int line_num = 0;
istringstream isstream;
for (vector<string>::iterator iter = svec.begin(); iter != svec.end(); ++iter)
{
cout << "line-" << ++line_num << ": " << endl;
isstream.str(*iter);
while (isstream >> word)
{
cout << word << endl;
}
isstream.clear();
}
return 0;
}
[chapter8]$ ./a.out
Enter filename:
testfile
OK: read to eof.
vector's size: 3
vector:
line1: How are you.
line2: How old are you.
line3: I'm OK.
line-1:
line1:
How
are
you.
line-2:
line2:
How
old
are
you.
line-3:
line3:
I'm
OK.