算法之字符串参数解析

 

 分析和思路:循环处理字符串,并更新处理后的字符串

 1 // ParameterParse.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
 2 //
 3 
 4 #include <iostream>
 5 
 6 #include "iostream"
 7 #include <sstream> 
 8 #include <vector>
 9 using namespace std;
10 
11 int main()
12 {
13     //思路:找到当前的"位置,把之前的字符处理,跳过当前双引号之内的字符,然后继续处理
14     string input;
15     
16     while (getline(cin, input))
17     {
18         vector<string> result;
19         string input_back = input;
20         while (input_back.size()> 0)
21         {
22             int npos = input_back.find_first_of('\"');
23             string sub = input_back.substr(0, npos);
24         
25 
26             stringstream ss(sub);
27             string buff;
28             while (getline(ss, buff, ' '))
29             {
30             //    cout << "buff: " << buff << endl;
31                 result.push_back(buff);
32             }
33             if (npos != -1)
34             {
35 
36 
37                 input_back = input_back.substr(npos);
38             //    cout << "input_back: " << input_back << endl;
39                 
40             }
41             else
42             {
43                 break;
44             }
45             int npos2 = input_back.find_first_of('\"');
46             if (npos2 != -1)
47             {
48                 string temp;
49                 int i = 0;
50                 for (i = npos2 + 1; input_back[i] != '\"'; i++)
51                 {
52                     temp.append(1, input_back[i]);
53                 }
54                 result.push_back(temp);
55             //    cout << "temp: " << temp << endl;
56                 input_back = input_back.substr(npos2 + i + 1);
57             //    cout << "input_back: " << input_back << endl;
58 
59             }
60         
61         
62         
63             
64             int a = 10;
65         }
66         int count = 0;
67         for (int i = 0; i < result.size(); i++)
68         {
69             if (result[i].size() != 0)
70                 count++;
71         }
72         cout << count << endl;
73         for (int i = 0; i < result.size(); i++)
74         {
75             if (result[i].size() != 0)
76                 cout << result[i] << endl;
77         }
78         
79     }
80 
81     return 0;
82 }

 

posted @ 2022-03-08 23:12  技术笔记记录  阅读(135)  评论(0)    收藏  举报