C++ stringstream格式化输出输入分析

发布时间:2021-11-12 13:47:47 作者:iii
来源:亿速云 阅读:291

这篇文章主要讲解了“C++ stringstream格式化输出输入分析”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C++ stringstream格式化输出输入分析”吧!

目录

最近在笔试时经常遇见各种输入问题,于是细心总结一波;首先string str; cin>>str;遇到空格结束;于是乎产生了getline(),可与得到一行字符串;空格自动去掉,只要不讲cin和getline混用即可

 cin.getline(s,k);

    接收一行中k个字符,可以接收空格
    cin.getline()实际有三个参数,cin.getline(字符串,接收个数,结束字符);
    当第三个参数省略时,系统默认为 '\0' ;

 getline(cin,s);

    和cin.getline()类似,读入一行字符串,值得注意的是cin.getline()属于istream流,而getline()属于string流,二者并不相同。

StringStream

这个东西单独讲,比较重要,包含在sstream库中。

然后stringstream的作用就是从string对象读取字符或字符串。

 

string s = "ABCD";
    stringstream ss(s);
    char ch;
    while(ss>>ch){
        cout << ch << " ";
    }

    //运行结果
    //A B C D

又例如:

string s = "hello world";
    stringstream ss(s);
    string str;
    while(ss>>str){
        cout << str << " ";
    }

    //运行结果
    //hello world

在某些题目需要处理字符串时,这些题目往往是输入的一行中包含多个字符以及空格,这个时候就可以利用 stringstream进行单个字符或者单个字符串分析处理了

  例子程序:

int main()
{

    string line;
    int k = 1;
    cout << "===============case1================" << endl;;
    while (getline(cin, line)) //可与读到包含空格, ;等字符;但是在ss>>x时被截断
    {
        int sum = 0, x;
        stringstream ss(line);
        while (ss >> x)
        {
            sum += x;
        }
        cout << "the sum is :" << sum << endl;
        ++k;
        cout << endl;
        cout << "===============case" << k << "================" << endl;;
    }

    return 0;
}

-输出:

===============case1================
1 2 3 4 5
the sum is :15

===============case2================
1,2,3,4,5
the sum is :1

===============case3================
1 a 2 b 3
the sum is :1

===============case4================
a 1 1 1 1
the sum is :0

===============case5================

另外一组:

int main()
{

    string line;
    int k = 1;

    cout << "===============case1================" << endl;;
    while (getline(cin, line))
    {
        string out, x;
        stringstream ss(line);
        while (ss >> x)
        {
            cout << x << ";";
        }
        ++k;
        cout << endl;
        cout << "===============case" << k << "================" << endl;;
    }

    return 0;
}

 输出:

===============case1================
this is very good!
this;is;very;good!;
===============case2================
this,is,very,good!
this,is,very,good!;
===============case3================

实验矩阵类型的输入:

3

0 1 2

2 3 4

5 6 7

int main()
{

    string line;
    int k = 1;
 //测试矩阵形式的输入:
    string input;
    int n;
    //cin >> n; //输入n行数据,如果后面用getline()后面的换行符不能处理
    getline(cin, input);
    stringstream ss(input);
    ss >> n;
    
    vector<vector<int> > vec;
    for (int i = 0; i < n;i++)
    {
        getline(cin,input); //会将换行符当做一行
        stringstream ss(input);
        int x;

        vector<int> temp;
        while (ss>>x) //只能以空格处理分离
        {
            temp.push_back(x);
        }
        vec.push_back(temp);
    }


    return 0;
}

 使用方法:

C++ stringstream格式化输出输入分析

经上面启发:练习这样的输入:

3 3
0,1 0,2;0,0 1,0;0,1 1,1;0,2 1,2;1,0 1,1;1,1 1,2;1,1 2,1;1,2 2,2;2,0 2,1

测试代码:

int main()
{

    string line;
    int k = 1;

    int row, col;
    getline(cin, line);
    stringstream ss(line);
    ss >> row >> col;
    getline(cin, line); //第二行
    stringstream s(line);
    string temp1, temp2, temp3;;
    vector<pair<pair<int,int>,pair<int,int>>> vec;
    while (getline(s, temp1, ';'))
    {
        vector<pair<int, int>> pair1;
        stringstream s3(temp1);
        while (getline(s3, temp2,' ')) //以换行符结束,中间为空格
        {
            pair<int, int> pair2;
            stringstream s4(temp2);
            vector<int> res;
            while (getline(s4, temp3, ','))
            {
                res.push_back(stoi(temp3));
            }
            pair2.first = res[0];
            pair2.second = res[1];

            pair1.push_back(pair2);
        }

        vec.push_back(make_pair(pair1[0],pair1[1]));
    }


    return 0;
}

输出:

C++ stringstream格式化输出输入分析

另外参考输入遇到过问题题解:

5 0
1 2 3
0 4
0 4
0 4
1 2 3

int main()
{
    int N, id;
    string str;
    getline(cin, str);
    stringstream ss(str);
    ss >> N >> id;

    vector<vector<int>> vec;
    //for (int i = 0; i < N; i++)
    //{
    //  vector<int> temp;
    //  int user;
    //  getline(cin, str);
    //  stringstream s(str);
    //  while (s>>user) //以空格进行划分
    //  {
    //      temp.push_back(user);
    //  }
    //  vec.push_back(temp);
    //}

    while (getline(cin, str)) //其实可都可以不知道行数
    {
        vector<int> temp;
        int user;
        stringstream s(str);
        while (s >> user)
        {
            temp.push_back(user);
        }
        vec.push_back(temp);
    }

    cout << recommendFriends(vec, id) << endl;

    return 0;
}

int test()
{
    //int N,id;
    //cin >> N >> id; //直接输入用户数和需要查找的用户id ; 这样就会产生换行符

    int N;
    vector<int> in;
    char c;
    while ((c = cin.get()) != '\n')
    {
        cin.unget();
        cin >> N; 
        in.push_back(N);
    }
    
    vector<vector<int>> vec;
    for (int i = 0; i < in[0]; i++)
    {
        vector<int> temp;
        int user;
        
        while ((c=cin.get())!= '\n') //文件结果没有换行符了,所以陷入死循环
        {
            cin.unget();
            cin >> user;
            temp.push_back(user);
        }
        if (temp.size()!=0)
        {
            vec.push_back(temp);
        }
        
    }

    cout << recommendFriends(vec, in[1]) << endl;

    return 0;
}

全部代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <unordered_set>

#include<sstream>
#include<fstream>
using namespace std;

#define cin infile //一定不能再oj系统中,有错,导致超时等!!!
//C++文件输入
ifstream infile("ini.txt", ifstream::in);


//函数功能:将输入字符串s,以字符串c(;)进行拆分,拆分结果放在v中
//函数参数说明:s为输入字符串;c为拆分的字符串;v为拆分结果
//函数返回值:正常返回0
int split_string(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
    std::string::size_type pos1, pos2;
    pos2 = s.find(c);
    pos1 = 0;
    while (std::string::npos != pos2)
    {
        v.push_back(s.substr(pos1, pos2 - pos1));

        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if (pos1 != s.length())
        v.push_back(s.substr(pos1));
    return 0;
}

int main()
{

    string line;
    int k = 1;
    /*cout << "===============case1================" << endl;;
    while (getline(cin, line))
    {
        int sum = 0, x;
        stringstream ss(line);
        while (ss >> x)
        {
            sum += x;
        }
        cout << "the sum is :" << sum << endl;
        ++k;
        cout << endl;
        cout << "===============case" << k << "================" << endl;;
    }*/

    //cout << "===============case1================" << endl;;
    //while (getline(cin, line))
    //{
    //    string out, x;
    //    stringstream ss(line);
    //    while (ss >> x)
    //    {
    //        cout << x << ";";
    //    }
    //    ++k;
    //    cout << endl;
    //    cout << "===============case" << k << "================" << endl;;
    //}

    ////测试矩阵形式的输入:
    //string input;
    //int n;
    ////cin >> n; //输入n行数据,如果后面用getline()后面的换行符不能处理
    //getline(cin, input);
    //stringstream ss(input);
    //ss >> n;
    //
    //vector<vector<int> > vec;
    //for (int i = 0; i < n;i++)
    //{
    //    getline(cin,input); //会将换行符当做一行
    //    stringstream ss(input);
    //    int x;
    //    vector<int> temp;
    //    while (ss>>x) //只能以空格处理分离
    //    {
    //        temp.push_back(x);
    //    }
    //    vec.push_back(temp);
    //}

    int row, col;
    getline(cin, line);
    stringstream ss(line);
    ss >> row >> col;
    getline(cin, line); //第二行
    stringstream s(line);
    string temp1, temp2, temp3;;
    vector<pair<pair<int,int>,pair<int,int>>> vec;
    while (getline(s, temp1, ';'))
    {
        vector<pair<int, int>> pair1;
        stringstream s3(temp1);
        while (getline(s3, temp2)) //以换行符结束,中间为空格
        {
            pair<int, int> pair2;
            stringstream s4(temp2);
            vector<int> res;
            while (getline(s4, temp3, ','))
            {
                res.push_back(stoi(temp3));
            }
            pair2.first = res[0];
            pair2.second = res[1];

            pair1.push_back(pair2);
        }

        vec.push_back(make_pair(pair1[0],pair1[1]));
    }


    return 0;
}

感谢各位的阅读,以上就是“C++ stringstream格式化输出输入分析”的内容了,经过本文的学习后,相信大家对C++ stringstream格式化输出输入分析这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. c++中如何使用stringstream unsigned char转16进制输出
  2. C++中如何实现I/O文件读写操作

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ stringstream

上一篇:python中逻辑回归限制的示例分析

下一篇:Django中的unittest应用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》