2016-03-25 29 views
0

depolanması Bu görevi belirli bir ölçüde gerçekleştiren bir kod yazım var. Ancak, kodumun nasıl değiştirileceğini öğrenmek isterim ki, kullanıcı vektöre girmek istediği çok sayıda dize girdisini saklayabilirim.C++ - Virgülle ayrılmış kullanıcı giriş dizesinin vektöre

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

Şimdiye kadar, sadece tanımak ve virgülle ayırarak iki giriş saklayabilirsiniz:

İşte benim kodudur. Kodlamada çok yeniyim, bu yüzden birisi bunu bir döngü haline getirmenin ve kullanıcı girdikçe çok sayıda girdi almanın bir yolunu gösterebilir mi?

Teşekkür ederiz.

+0

Bu için çalışmaz 'bu benim liste, a, b, c, d olduğunu satırın sonu. – Shark

+1

Sorun, for döngüsündedir; Her yinelemeden sonra vektöre A ve B'yi itmiyorsunuz. Her iterasyonun sonunda dizeleri vektöre koymanız, ardından tekrarlamanız gerekir. @Shark'ın işaret ettiği gibi mantığınızda küçük sorunlar da var. –

cevap

1

Herhangi bir sayıda kullanıcı girişi için çalışmak için kodunuzu değiştirmeniz gerekir. Mantık, her alt dizeyi virgüller arasında vector'a itmektir.

Vektörü yazdırmak için öncelikle vektörün boyutunu bulmanız gerekir, ardından yazdırmak için basitçe yineleyin.

//display 

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

Not: Eğer kod örneğin arasına boşluk dizeleri için çalışmak isterseniz: a ,b ,c ,d sonra kullanıcıdan girdi almak getline(cin,input); kullanın.

0

running code here veya github gist olarak görebilirsiniz.

// 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 tarafından önerildiği gibi, stringstream kullanımı çok daha iyi. Özel bir durum işleme gerekli değildir! Bir süre döngü kullanıyorum - neler olduğunu daha net görünüyor.

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 kullanarak bir giriş dizesi ayrıştırmak için çok daha basit yaklaşım vardır:

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

İlgili konular