zoukankan      html  css  js  c++  java
  • C++:istringstream 的用法

    摘自:http://www.3gtarena.com/danapeixunjishu/C__peixun/2048.html

    今天看到了一个比较有用的c++的输入输出控制类。C++引入了ostringstream、istringstream、stringstream这三个类,要使用他们创建对象就必须包含<sstream>这个头文件。

        istringstream类用于执行C++风格的串流的输入操作。

        ostringstream类用于执行C风格的串流的输出操作。

        strstream类同时可以支持C风格的串流的输入输出操作。

        istringstream的构造函数原形如下:

        istringstream::istringstream(string str);

        它的作用是从string对象str中读取字符。

       

    View Code
    #include<iostream>
    
        #include<sstream>        //istringstream 必须包含这个头文件
    
        #include<string>
    
        using namespace std;
    
        int main()
    
        {
    
        string str=“i an a boy”;
    
        istringstream is(str);
    
        string s;
    
        whileis》s)
    
        {
    
        cout《s《endl;
    
        }
    
        }

        输出是:

        i

        am

        a

        boy 

    摘自:http://www.cppblog.com/shyli/archive/2006/10/17/13758.html

    View Code
    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;
        }    
        return 0;
    }

    测试:
    input:
    abc   df   e              efgeg      ffg

    ouput:
    abc
    df
    e
    efgeg
    ffg

  • 相关阅读:
    vfs:结构体对象
    vfs:open.c 源码学习
    Linux direct io使用例子
    GPU安装小结
    tensorflow scope的作用
    tensorflow 一维卷积 tf.layers.conv1()使用
    tensorflow 的tf.where详解
    tensorflow 的tf.split函数的用法
    tensorflow 添加一个全连接层
    tensorflow 计算均值和方差
  • 原文地址:https://www.cnblogs.com/KeenLeung/p/2966447.html
Copyright © 2011-2022 走看看