关于实用的 stringstream

处理毒瘤输入数据

比如这个题
在输入的时候有很多问题,如果用scanf输入char型字符串,那么不好用map判断,并且读入整行判断换行会很麻烦
如果选择用string型数组处理数据,除了用getline,不然很难判断换行
然而stringstream可以完美的解决这个问题

//这是上面介绍的第一个用法  ,PS: 输出放下面了(雾
//解释一下——
//stringstream是一个输入输出流,它可以模拟cin和cout的操作
//你可以在流里面存东西,也可以从里面把东西拿出来
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
    stringstream ss;
    string a;
    string b = "China";
    string c;
    string d;
    string e;
    //"----------下面是stringstream的各种操作-----------"

    // 1.存入
     ss << b;//给 ss 流传东西 

     ss >> a;//把 ss 流的东西传出去
     cout << "a: " << a << endl;

     ss >> c;
     cout << "c: " << c << endl;//会发现c没有输出任何东西,因为 ss 流里面的字符串已经传给 a 了

    //另外的存入方法
     ss.clear();//用完必须清理一下
     ss.str("heanda");//括号里面也可以是string型变量
     ss >> d;
     cout<< "d: " << d << endl;

     ss.clear();

     //接下来就看看stringstream存入输出的用法
     ss.str("had is chinese");
     while(ss >> e) cout << "e: " << e << endl;//它会把空格作为一个断点,一个一个传出去,所以可以一个一个取

    return 0;

        /*
        输出:
        a: China
        c:
        d: heanda
        e: had
        e: is
        e: chinese
        */
}

很方便的类型转换

除了处理毒瘤数据,它还可以进行一些类型转换
1.数字型 ——> string字符串
2.string字符 ——> 数字型

#include <iostream>
#include <cstdio>
#include <sstream>
using namespace std;
int main()
{
    double a = 1.00;
    float b = 3.141592;
    int c;
    stringstream ss;

    //1.将string丢给成int

    string s = "1234";
    ss.str(s);
    ss >> c;

    cout << "string->int: " << c << endl;

    ss.clear();
    ss.str("");

    //2.将小数丢给stringstream
    ss << a;
    string s2;
    ss >> c;

    cout << "double -> int: " << c << endl;

    ss.clear();
    ss.str("");

    //3.将小数丢给小数
    ss.setf(ios::fixed);
    ss.precision(2);
    ss << b;
    double number;
    ss >> number;

    cout << "string->double 并且控制小数位数: " << number << endl;

    ss.clear();
    ss.str("");

    //4.数字->字符串
    int num = 2003;
    ss << num;

    string num_s;

    ss >> num_s;

    cout << "int -> string: " << num_s << endl;

    /*// 输出在这
    string->int: 1234
    double -> int: 1
    string->double 并且控制小数位数: 3.14
    int -> string: 2003
    */

    return 0;
}

最新文章

  1. Google C++单元测试框架GoogleTest---GTest的Sample1和编写单元测试的步骤
  2. 封装ajax(二)闭包的形式
  3. npm Scripts使用教程【译】
  4. bootstrap fileinput 文件上传工具
  5. (转)深入理解javascript的function
  6. 用JS实现避免重复加载相同js文件
  7. javascript——函数内部属性
  8. JavaScript 原型与原型链
  9. Pain for friend
  10. UI状态控制
  11. Java字符串的split(String str)方法空串的问题
  12. ●线段树题之wows
  13. 使用Docker镜像和仓库
  14. vue结构详解
  15. Spring MVC知识
  16. Redis主从集群及哨兵模式
  17. Mnist
  18. 解决项目无法添加VBIDE问题
  19. 手动配置SVN服务
  20. 任务取消TASK

热门文章

  1. nginx配置静态资源与动态访问分离【转】
  2. 空指针异常 自动拆箱 防止 NPE,是程序员的基本修养 本手册明确防止 NPE 是调用者的责任。
  3. JDBC DataSource
  4. JDK动态代理在RPC框架中的应用
  5. 使用SoapUI发送Post请求
  6. VPB测试 使用Osgdem运行例子
  7. matlab基本函数find
  8. Spring Cloud与Docker微服务架构实战 PDF版 内含目录
  9. 报错:Connection to node -1 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
  10. 基于grafana+prometheus构建Flink监控