• 处理算法笔试中的格式化字符串输入


    如何处理算法笔试中的格式化字符串输入。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>//replace函数需要
     4 #include <sstream>  //stringstream需要
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     /*读取成数组的例子*/
    10     string str;
    11     cin >> str;                                //先读入字符串
    12     replace(str.begin(), str.end(), ',', ' '); //逗号替换空格
    13 
    14     stringstream ss;
    15     ss.str(str);//转换成stringstream
    16 
    17     int i = 0;
    18     vector<int> vec(100);
    19     while (ss >> vec[i])//流格式化
    20         i++;
    21     int size = i;
    22     for (i = 0; i < size; i++)
    23         cout << vec[i] << "  ";
    24 
    25     /*读取成字符串的例子*/
    26     string str1;
    27     cin >> str1;                                 //先读入字符串
    28     replace(str1.begin(), str1.end(), ',', ' '); //逗号替换空格
    29 
    30     stringstream ss1;
    31     ss.str(str1);//转换成stringstream
    32 
    33     int i1 = 0;
    34     vector<string> vec1(100);
    35     while (ss >> vec1[i1])//流格式化
    36         i1++;
    37     int size1 = i1;
    38     for (i1 = 0; i1 < size; i1++)
    39         cout << vec1[i] << "  ";
    40 
    41     //模板真的强大
    42     return 0;
    43 }

    今天做算法题,不会处理输入......丢人了!

  • 相关阅读:
    给存储过程传递一个表
    Linker problems with Borland builder
    Python内置函数super的不便之处
    接口测试基础
    接口测试工具篇postman
    接口测试工具篇jmeter
    git的使用
    git与pycharm结合使用
    抓包工具fiddler
    sql 中 case when 语法
  • 原文地址:https://www.cnblogs.com/yocichen/p/11432631.html
Copyright © 2020-2023  润新知