【问题标题】:C++ Split a string by blank spaces unless it is enclosed in quotes and store in a vector [duplicate]C ++用空格分割字符串,除非它用引号引起来并存储在向量中[重复]
【发布时间】:2018-03-14 02:16:45
【问题描述】:

我有一个需要用户输入的提示。我想接受这个用户输入并将每个单词存储在一个向量中,用空格分隔,除非引号之间包含一组单词,在这种情况下,我希望引号中的所有单词都计为 1。

例如,如果用户输入以下内容:

12345 Hello World "This is a group"

然后我要存储向量:

vector[0] = 12345
vector[1] = Hello
vector[3] = World
vector[4] = "This is a group"

我有以下代码,它通过空格分割用户输入并将其存储在一个向量中,但我无法弄清楚如何使引号内的所有文本都算作一个。

 string userInput

 cout << "Enter a string: ";
 getline(cin, userInput);

string buf; 
stringstream ss(userInput); 

vector<string> input;

while (ss >> buf){
    input.push_back(buf);

我想在用户输入引号的单词周围保留引号。我还想将结果保存到向量中,而不仅仅是将字符输出到屏幕上

【问题讨论】:

    标签: c++ string c++11 vector split


    【解决方案1】:

    C++14 已内置:http://en.cppreference.com/w/cpp/io/manip/quoted

    Live On Coliru

    #include <string>
    #include <vector>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    
    int main(void) {
        std::istringstream iss("12345 Hello World \"This is a group\"");
        std::vector<std::string> v;
        std::string s;
    
        while (iss >> std::quoted(s)) {
            v.push_back(s);
        }
    
        for(auto& str: v)
            std::cout << str << std::endl;
    }
    

    打印

    12345
    Hello
    World
    This is a group
    

    【讨论】:

    • 我说 OP 被误导了 :) std::quoted 也可以用其他方式 std::cout &lt;&lt; std::quoted("Whoah, that's \"cool\"!");
    【解决方案2】:

    这是一个工作示例:

    #include <string>
    #include <vector>
    #include <iostream>
    using namespace std;
    int main(void) {
        string str = "12345 Hello World \"This is a group\"";
        vector<string> v;
        size_t i = 0, j = 0, begin = 0;
        while(i < str.size()) {
            if(str[i] == ' ' || i == 0) {
                if(i + 1 < str.size() && str[i + 1] == '\"') {
                    j = begin + 1;
                    while(j < str.size() && str[j++] != '\"');
                    v.push_back(std::string(str, begin, j - 1 - i));
                    begin = j - 1;
                    i = j - 1;
                    continue;
                }
    
                j = begin + 1;
                while(j < str.size() && str[j++] != ' ');
                v.push_back(std::string(str, begin, j - 1 - i - (i ? 1 : 0) ));
                begin = j;
            }
            ++i;
        }
    
        for(auto& str: v)
            cout << str << endl;
        return 0;
    }
    

    输出:

    12345
    Hello
    World
    "This is a group"
    

    但是,请注意,此代码用于演示,因为它不能处理所有情况。例如,如果 yuo 在您的输入中只有双引号,那么这个 while(j &lt; str.size() &amp;&amp; str[j++] != '\"'); 将从该点开始的整个字符串不被拆分。

    【讨论】:

    • @gsamaras 知道为什么如果我尝试像 if v[1] == "Hello" ... do something 这样的东西不起作用?
    • @MastRofDsastR = 用于分配,== 用于比较。你错过了吗?
    • @gsamaras,道歉,意思是==
    • 我不确定@MastRofDsastR 在问我什么。
    猜你喜欢
    • 2012-11-17
    • 2013-12-13
    • 2013-05-31
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 2011-01-28
    • 2013-03-03
    • 2011-03-26
    相关资源
    最近更新 更多