zoukankan      html  css  js  c++  java
  • #include <sstream>

    1 std::istringstream

    2 std::stringstream

    1 std::istringstream

    input

    1 在一个字符串string里提取部分数据,这些数据以空格' '为间隔。

    以空格' '为间隔,可以直接提取

     1 #include <iostream>
     2 #include <sstream>
     3 
     4 struct MyStruct
     5 {
     6     std::string str1, str2, str3;
     7     double db;
     8     int num;
     9     char ch;
    10 };
    11 
    12 void main()
    13 {
    14     std::string mystring("china google microsoft 12.9 123 A");//创建一个字符串,用于提取
    15     MyStruct struct1;//创建一个结构体,用于保存结果
    16 
    17     std::istringstream input(mystring);//创建一个字符串扫描流
    18     input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;//扫描
    19     
    20     std::cout << struct1.str1 << std::endl;//打印
    21     std::cout << struct1.str2 << std::endl;
    22     std::cout << struct1.str3 << std::endl;
    23     std::cout << struct1.db << std::endl;
    24     std::cout << struct1.num << std::endl;
    25     std::cout << struct1.ch << std::endl;
    26 
    27     system("pause");
    28 }

    2 在一个字符串string里提取部分数据,这些数据以'#'为间隔。

    以'#'为间隔,要多处理一步,先把'#'查找和替换成空格' ',再提取

     1 #include <iostream>
     2 #include <sstream>
     3 
     4 struct MyStruct
     5 {
     6     std::string str1, str2, str3;
     7     double db;
     8     int num;
     9     char ch;
    10 };
    11 
    12 void main()
    13 {
    14     char mystring[] = "china#google#microsoft#12.9#123#A";//创建一个字符串,用于提取
    15     
    16     for (char *p = mystring; *p != ''; p++)//查找和替换#
    17     {
    18         if (*p == '#')
    19         {
    20             *p = ' ';
    21         }
    22     }
    23 
    24     MyStruct struct1;//创建一个结构体,用于保存结果
    25 
    26     std::istringstream input(mystring);//创建一个字符串扫描流
    27     input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;//扫描
    28 
    29     std::cout << struct1.str1 << std::endl;//打印
    30     std::cout << struct1.str2 << std::endl;
    31     std::cout << struct1.str3 << std::endl;
    32     std::cout << struct1.db << std::endl;
    33     std::cout << struct1.num << std::endl;
    34     std::cout << struct1.ch << std::endl;
    35 
    36     system("pause");
    37 }

    2 std::stringstream

    使用&拼接字符串,执行两个system命令

     1 #include <iostream>
     2 #include <sstream>
     3 
     4 void main()
     5 {
     6     std::stringstream mystr;//字符串进行输入
     7 
     8     char cmd1[30] = { 0 };//创建字符串
     9     char cmd2[30] = { 0 };//创建字符串
    10 
    11     std::cin.getline(cmd1, 30).getline(cmd2, 30);//字符串赋值
    12 
    13     mystr << cmd1 << '&' << cmd2;//输出到mystr
    14 
    15     std::string str = mystr.str();//转换
    16     
    17     system(str.c_str());//执行
    18     
    19     system("pause");
    20 }
  • 相关阅读:
    求多边形的面积
    Sequence operation3397
    Atlantis1542(线段树求矩形覆盖面积)
    hdu3033 分组背包(每组最少选一个)
    poj3468A Simple Problem with Integers(线段树延时更新)
    Picture 1828
    Minimum Inversion Number 1394(线段树法)
    hdu2955 Robberies 01背包
    C# 对MongoDB数据库进行增删该
    C#连接MongoDB数据库应用实战
  • 原文地址:https://www.cnblogs.com/denggelin/p/5675354.html
Copyright © 2011-2022 走看看