zoukankan      html  css  js  c++  java
  • stringstream函数(i o)

    stringstream函数

    头文件  #include<sstream>

    stringstream是字符串流,被用来切分数据或转化类型

    样例一(摘)

    输入n,代表接下来输入n行资料,每行资料有不固定个数的整数(最多20个,不大于200个字元)。输出每行的总数

    输入:

    3 1 2

    3 20 17 23 54 77 60

    111 222 333 444 555 666 777 888 999

    输出:

    6

    251

    4995

    代码

    #include<iostream>
    #include<string>
    #include<sstream>
    using namespace std;
    int main()
    {
    string s;
    stringstream ss;
    int n, i, sum, a;
    cin >> n;
    getline(cin, s);           //读取换行
    for (i=0; i<n; i++){
        getline(cin, s);
        ss.clear();             //清空
        ss.str(s);            //用str()将指定字串s设为开始的内容
        sum=0;
        while (1)
        {
            ss >> a;
            if ( ss.fail() ) break;
            sum+=a;
        }
        cout << sum << endl;
    }
    //system("pause");
    return 0;
    }

    样例二(摘)

    基本数据类型转换例子 int转string

    #include <string>
     #include <sstream>
     #include <iostream> 
    using namespace std;
     int main()
     {
        stringstream ss;
         string result;
         int i = 1000;
         ss << i;             //将int输入流
        ss >> result;           //从stream中抽取前面插入的int值
        cout << result << endl;    // print the string "1000"
        //system("pause");
        return 0;
     } 

    样例三(摘)

    int转换为char *

    #include <sstream>
     #include <iostream> 
    using namespace std;
     int main()
     {
         stringstream ss;
         char result[8] ;
         ss << 8888;                   //向stream中插入8888
         ss>> result;                   //抽取stream中的值到result
         cout << result <<endl;          // 屏幕显示 "8888"
        //system("pause");
        return 0;
     } 

    样例四(摘)

    多次转换时必须调用成员函数clear()

    #include <sstream>
     #include <iostream> 
    using namespace std;
     int main()
     {
        stringstream ss;
         int first, second;
         ss<< "123456";                 //插入字符串
        ss >> first;                //转换成int
         cout << first <<endl;
         ss.clear();                 //在进行多次转换前,必须清除stream
         ss<< false;                 //插入bool值
        ss>> second;                //提取出int
        cout << second <<endl;
        //system("pause");
        return 0;
     } 

    istringstream

    预测分析算法是一些入栈出栈操作,问题是当产生是匹配时,要把该产生式的体反向入栈,但是这些文法符号是string类型而不能是char的,该怎样入栈呢,网上找了好多,发现了这个类istringstream,头文件要包含ssream,简单用法如下:

    #include<iostream>
    #include<sstream>
    #include<string>
    using namespace std;
    int main()
    {
        string str="i an a boy";
        istringstream is(str);
        string s;
        while(is>>s)
        {
            cout<<s<<endl;
        }
        //system("pause");
        return 0;
    }

    istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。

    #include<iostream>
    #include<sstream>
    using namespace std;
    int main()
    {
        string str, line;
        while(getline(cin, line))
        {
            istringstream stream(line);
            while(stream>>str)
                cout<<str.c_str()<<endl;
        }    
         //system("pause");
        return 0;
    }

    Sample input

    a ddjfksj        jkdsnfsfn  nd

    a

    Sample output

    ddjfksj

    jkdsnfsfn

    nd

    总结

    •   istringstream,由 istream 派生而来,提供读 string 的功能。 输入操作
    •   ostringstream,由 ostream 派生而来,提供写 string 的功能。 输出操作
    •   stringstream,由 iostream 派生而来,提供读写 string 的功能。输入输出操作

    istringstream的构造函数原形: istringstream::istringstream(string str);

    stringstream  特定的操作 
    
    stringstream strm; // 创建自由的 stringstream 对象
    stringstream strm(s); //创建存储 s 的副本的 stringstream 对象,其中 s 是 string 类型的对象
    strm.str()  //返回 strm 中存储的 string 类型对象
    strm.str(s)  //将 string 类型的 s 复制给 strm,返回 void 

    stringstream  提供的转换和/或格式化
    stringstream 对象的一个常见用法是,需要在多种数据类型之间实现自动格式化时使用该类类型。

    例如,有一个数值型数据集合,要获取它们的 string 表示形式,或反之。sstream 输入和输出操作可自动地把算术类型转化为相应的 string 表示形式,反过来也可以。

  • 相关阅读:
    mysql ibd 文件过大问题
    magento性能分析插件
    magento 自定义url路径 和 filter data 小结
    magento layout xml 小结
    magento 开启 3D secure credit card validation
    magento package
    docker安装与使用记录(debian9)
    Windows使用Charles对模拟器/真机进行抓包 问题记录
    windows10 windump使用记录
    使用systrace的问题记录
  • 原文地址:https://www.cnblogs.com/farewell-farewell/p/5231812.html
Copyright © 2011-2022 走看看