C++内核格式化

内核格式化:
    头文件ssstrem定义了一个从ostream类派生而来的ostingstream类
    (还有一个基于wostream的wostringstream类,这个类用于宽字集)
    如果创建一个ostringstream的对象,则可以将信息写入其中,它将储存这些信息。
    可以将可以用于cout的方法用于ostringstream对象
 
    可以执行下面操作:
    ostringstream outstr;
    double price=380.0;
    char* ps =" for a copy of the  ISO/EIC C++ standard";
    outstr<<fixed;
    outstr<<"Play only CHF"<<price<<ps<<endl;
 
案例:
    #include<iostream>
    #include<sstream>
    #include<string>
    using namespace std;
    int main()
    {
        ostringstream outstr;
        string hdisk;
        cout << "What's the name of your disk?";
        getline(cin, hdisk);
        int cap;
        cout << "What's its cappacity in GB?";
        cin >> cap;
        outstr << "The hard disk " << hdisk << " has a capacity of "
            << cap << " gigabytes. ";
        string result = outstr.str();
        cout << result;
        return 0;
    }
 
 
    istingstream类允许使用istream 方法族获取 istingstream 对象中的数据
    istingstream对象可以使用string对象进行初始化
 
    string facts;
    istingstream instr(facts);
    可以使用istream 方法读取instr中的数据。
    int n;
    int sum=0;
    while(instr>>n)
    {
        sum+=n;
    }
 
使用重载的>>运算符读取字符串中的内容,每次读取一个单词;
    #include<iostream>
    #include<sstream>
    #include<string>
    using namespace std;
    int main()
    {
        string lit="It was a  dark and stormy day,and the full moon glowed brilliantly.";
        istingstream instr(lit);
        string word;
        while(instr>>word)
        {
            cout<<word<<endl;
        }
        return 0;
    }
原文地址:https://www.cnblogs.com/X-dream/p/5152437.html