c++ istringstream用法

istringstream用法,见如下代码

#include <iostream>
#include"sstream"
using namespace std;
int main() {
    istringstream is("I am\tlove C++\nand I love C");
    string str;
    //默认是空格或者回车符或者tab,会输出如下内容:I am love C++ and I love C
    while (is>>str){
        cout<<str<<" ";
    }
    cout<<endl;
    istringstream istringstream1("30+2*2+3-");
    long ans;
    char op;
    istringstream1>>ans;
    //会得到如下内容 30 + 2 * 2 + 3 -
    while (istringstream1 >> op) {
        cout<<ans<<" "<<op<<" ";
        istringstream1>>ans;
    }
    cout<<endl;

    istringstream istringstream2("20q230r4");
    istringstream2>>ans;
    //会得到如下内容  20 q 230 r
    while (istringstream2>>op){
        cout<<ans<<" "<<op<<" ";
        istringstream2>>ans;
    }
    return 0;
}

 

 

posted @ 2019-12-05 14:59  suwenyuan  阅读(1351)  评论(0编辑  收藏  举报