2016-03-25 13 views
0

このタスクをある程度実行するコードがあります。しかし、私はどのように私はあなたがベクトルに入力したい多くの文字列入力を格納することができるように自分のコードを変更したいと思います。ここでC++ - コンマで区切られたユーザー入力文字列をベクトルに格納する

は私のコードです:

#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; 
} 

これまでのところ、それが唯一のカンマで区切られた2つの入力を認識し、保存することができます。私はコーディングに非常に新しいので、誰かがこれをループにして、ユーザーが入力するのと同じくらい多くの入力を取り入れる方法を私に示すことができますか?

ありがとうございます。

+0

これは 'これは私のリストであり、A、B、C、Dのために動作しません。 、end of line.' – Shark

+1

問題はあなたのforループにあります。各反復後にAとBをベクトルに押し込んでいるわけではありません。各繰り返しの終わりに文字列をベクトルに入れてから、繰り返す必要があります。 @Sharkが指摘したように、あなたのロジックには小さな問題もあります。 –

答えて

1

任意の数のユーザー入力を処理するには、コードを変更する必要があります。 論理は、コンマの間のすべてのサブ文字列を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; 
    } 
} 

ベクターを印刷するには、ベクターのサイズを見つけてから、それを繰り返して印刷するだけです。

//display 

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

注:あなたのコードは、例えば、間にスペースを含む文字列のために仕事をしたい場合:a ,b ,c ,dは、ユーザからの入力を取るためにgetline(cin,input);を使用しています。

0

running code hereまたは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; 

} 

Christopheが提案したように、ストリングストリームを使用する方がはるかに優れています。特殊なケースハンドリングは必要ありません!私は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); 
} 
2

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

関連する問題