简体   繁体   English

C ++-将用逗号分隔的用户输入字符串存储到向量中

[英]C++ - Storing user input string separated by commas into vector

I have a code written that performs this task to a certain extent. 我编写了可以在一定程度上执行此任务的代码。 But, I would like to how to alter my code so that I can store as many string inputs the user wants to enters into the vector. 但是,我想如何更改代码,以便可以存储用户想要输入到向量中的尽可能多的字符串输入。

Here is my code: 这是我的代码:

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

int main ()
{
string input = "";
cout << "Input: ";
cin >> input;
string a,b;

for(int i = 0; i<input.size(); i++)
{
    if(input.at(i)==','){
        a=input.substr(0,i);
        b=input.substr(i+1);
    }
}

vector<string> objects;
objects.push_back(a);
objects.push_back(b);

for (int k = 0; k < 2; k++) {
    cout << objects[k] << endl;
}

return 0;
}

So far, it can only recognize and store two inputs separated by commas. 到目前为止,它只能识别和存储两个用逗号分隔的输入。 I am very new to coding so could someone show me a way to make this into a loop and take in as many inputs as the user enters? 我对编码非常陌生,因此有人可以向我展示一种使之进入循环并接受用户输入的尽可能多输入的方法吗?

Thank you. 谢谢。

There are much simpler approaches to parse an input string using stringstreams: 使用stringstreams解析输入字符串的方法要简单得多:

string a;
vector<string> objects;

for(stringstream sst(input); getline(sst, a, ','); )  // that's all ! 
    objects.push_back(a);

copy (objects.begin(), objects.end(), ostream_iterator<string>(cout," ; "));  // display all

Online demo 在线演示

You need to change your code in order to work for any number of user input. 您需要更改代码才能适用于任意数量的用户输入。 The logic is to push every sub string between the commas into vector . 逻辑是将逗号之间的每个子字符串都推到vector

vector<string> objects;

for(int i = 0,j=0; i<input.size(); i++)
{
    if(input.at(i)==',' || input.at(i)=='\0'){
        objects.push_back(input.substr(j,i-j)); //pushing the sub string
        j=i+1;
    }
}

In order to print the vector first you have to find the size of the vector,then simply iterate over to print it. 为了首先打印矢量,您必须找到矢量的大小,然后简单地遍历即可打印。

//display

int l=objects.size();
for (int k = 0; k < l; k++) {
    cout << objects[k] << endl;
}

Note: If you want your code to work for strings with spaces in between , for example: a ,b ,c ,d then use getline(cin,input); 注意:如果要使代码适用于之间使用空格的字符串,例如: a ,b ,c ,d则使用getline(cin,input); to take input from user. 接受用户的输入。

You can see running code here or as a github gist . 您可以在此处github gist中查看正在运行的代码

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <string>

void ParseCSV(
    std::vector< std::string >& output,
    const std::string& csv )
{

    int q = 0;
    int p = csv.find(",");
    while( p != -1 )
    {
        output.push_back( csv.substr(q,p-q) );
        q = p+2;
        p = csv.find(",",q);
    }

    // The terminating comma of the CSV is missing
    // so we need to check if there is
    // one more value to be appended

    p = csv.find_last_of(",");
    if( p != -1 )
    {
        output.push_back( csv.substr( p+2 ) );

    }
    else
    {
        // there was no comma
        // this could be because the list is empty
        // it could also be because there is just one element in the list

        if( csv.length() > 1 )
            output.push_back( csv );
    }
}

int main()
{
    std::string test("this is my list, a, b, c, d, end of line");
    std::vector< std::string > split;
    ParseCSV( split, test );
    for( auto& s : split )
        std::cout << s << std::endl;

}

As suggested by Christophe, using stringstream is much better. 正如Christophe所建议的那样,使用stringstream更好。 No special case handling needed! 无需特殊情况处理! I use a while loop - it seems clearer what is happening. 我使用一个while循环-似乎更清楚发生了什么。

void ParseCSV2(
    std::vector< std::string >& output,
    const std::string& csv )
{
    std::stringstream sst(csv);
    std::string a;
    while( getline( sst, a, ',' ) )
        output.push_back(a);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM