<C++>istringstream, ostringstream, stringstream的用法

来源:互联网 发布:linux tomcat debug 编辑:程序博客网 时间:2024/05/17 23:00

1、ostringstream:ostringstream 对象用来进行格式化的输出,可以方便的将各种类型转换为string类型。ostringstream 只支持 << 操作符。
2、istringstream :istringstream 对象用来把一个已定字符串中的以空格隔开的内容提取出来。istringstream 只支持 >> 操作符。
3、stringstream: stringstream类就是上述istringstream和ostringstream类的综合,支持<<, >>操作符,可以进行字符串到其它类型的快速转换。
4、这几个类istringstream, ostringstream, stringstream, 包含在 sstream 文件里。

输入方式:
1.构造函数。iss, oss, ss 都支持。istreamstring iss(Input), ostreamstring(Input), stringstream ss(Input)。
2.<< 操作符。oss, ss 支持。 oss << Input, ss << Input;
3.Str()函数,提供C中的字符串头指针,或者匿名内存首地址
iss.str(Input.c_str()), iss.str(“abc 123 345”)
oss.str(Input.c_str()), oss.str(“abc 123 345”)
ss.str(Input.c_str()), ss.str(“abc 123 345”)

输出方式:
1. 通过.str()得到一份拷贝
Str1 = iss.str(), Str1 = oss.str(), Str1 = ss.str();
显然,这个也提供C语言的接口
printf(“%s”, iss.str().c_str()), printf(“%s”, oss.str().c_str()), printf(“%s”, ss.str().c_str())
2.>>操作符, iss和ss支持该操作

每次不同的使用时刻必须要清空,oss.clear() 并不能清空内存
ss.str(“”), iss.str(“”), oss.str(“”),暂时没有发现其它的清空方法。

最大的用处
输入string类型,可以通过iss 或 ss 类提取出来,功能类似于C语言中的sscanf()函数, 在TC中比较常见
提供字符串和各种类型之间的转换。

//*******  istringstream用法  **************************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    istringstream iss;    string istr1, istr2, istr3;    int a;    //输入字符串    iss.str("I love C");    iss >> istr1 >> istr2 >> istr3;    cout << istr1 << " " << istr2 << " " << istr3 << endl;    // 字符串转换为int    iss.clear();    iss.str("1534");    iss >> a;    cout << a << endl;    //用C测试转换    printf("%d\n", a);    return 0;}

输出:
I love C
1534
1534

//********* ostringstream用法 **********************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    ostringstream oss;    string ostr1, ostr2, ostr3;    string ostr;    string strFromInt;    string strFromDouble;    //输出字符串    ostr1 = "My ";    ostr2 = "Name's ";    ostr3 = "C";    oss << ostr1;    oss << ostr2;    oss << ostr3;    cout << oss.str() << endl;    //int 转化为string    oss.str("");    oss << 23412;    cout << oss.str() << endl;    //double 转化为string    oss.str("");    oss << 1.2345;    cout << oss.str() << endl;    //将string型赋给char型。     关键:c_str函数的返回值是const char*的,不能直接赋值给char*,必须赋给const char*    const char *I = strFromInt.c_str();    const char *D = strFromDouble.c_str();    printf("%s,%s\n", I, D);    return 0;}

输出:
My Name’s C
23412
1.2345

//*********** stringstream *********************************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    int a, b;    string Str1, Str2;    string Input = "abc 123 bcd 456 sss 999";    stringstream ss;    ss << Input;    while(ss >> Str1 >> a)    {        cout << Str1 << " " << a << endl;    }}

输出:
abc 123
bcd 456
sss 999

//*********** getline(cin, line) *********************************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    string line, word;    while (getline(cin, line))   //getline(cin ,s)   读取一行输入的字符串,包括空格  字符串流是通过空格判断一个字符串的结束    {        stringstream stream(line);        cout << stream.str() << endl;             while (stream >> word){ cout << word << endl; }    }    return 0;}

输入:1 2 3 4
输出:
1 2 3 4
1
2
3
4

//********** 字符串流是通过空格判断一个字符串的结束 **************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    int val1 = 512, val2 = 1024;    stringstream ss;    ss << "val1: " << val1 << endl          //“val1: "        << "val2: " << val2 << endl;    cout << ss.str();    string dump;    int a, b;    ss >> dump >> a        >> dump >> b;    cout << a << " " << b << endl;    return 0;    }

输出:
val1: 512
val2: 1024
512 1024

//************* ss.str("") *********************//#include <iostream>#include <string>#include <sstream>using namespace std;int main(){    stringstream ss;    string s;    ss << "shanghai no1 school";    ss >> s;    cout << "size of stream = " << ss.str().length() << endl;    cout << "s: " << s << endl;    ss.str("");        cout << "size of stream = " << ss.str().length() << endl;    return 0;}

输出:
shanghai no1 school
size of stream = 19
s: shanghai
size of stream = 0

另外可参考: istringstream, ostringstream, stringstream用法示例

1 0