2012-02-10 33 views
1

私はC++を初めて使用しています。文字列を区切り文字で分割し、その部分文字列をベクトルに入れることに問題があります。空の文字列をベクトルに格納する方法

vector<string> split(const string &s, const string &delim) 
{ 
    string::size_type pos = s.find_first_of(delim,0); 
    int start = 0; 
    vector<string> tokens; 

    while(start < s.size()) 
    { 
      if(start++ != pos + 1) 
        tokens.push_back(" "); 
      pos = s.find_first_of(delim, start); 
      tokens.push_back(s.substr(start, pos - start)); 
    } 

    for(vector<string>::size_type i = 0; i != tokens.size(); ++i) 
      cout << tokens[i]; 

    return tokens; 
} 

文字列と区切り文字が関数に渡されており、分割を行う次のよう

私のコードです。この関数は、空の文字列をベクトルに入れることを想定していますが、私にとってはそれをしません。例えば

私のようにメイン内の関数を呼び出す場合:出力

int main() 
{ 
    split("<ab><>cd<", "<>"); 
} 

"","ab","","","","cd","" 

マイナス引用符

すると仮定が、私のコードの出力は、現在あるさ

ab b cd d 

助けていただければ幸いです。

+0

が役立つかもしれないいくつかの関連の質問です:http://stackoverflow.com/questions/236129/how-to-split-a-string-in-cます。http:/ /stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c http://stackoverflow.com/questions/4533652/how-to-split-string-using-istringstream-with-その他の区切り文字(空白以外)http://stackoverflow.com/questio ns/5505965/fast-string-split-with-multiple-delimiters –

答えて

1

このトリックん...

#include <iostream> 
#include <vector> 

using namespace std; 

vector<string> split(string record, string token) { 
    vector<string> results; 
    size_t startPos = 0; 
    size_t pos = 0; 

    // Step: If either argument is empty then return 
    // an empty vector. 
    if (record.length() == 0 || token.length() == 0) { 
     return results; 
    } 

    // Step: Go through the record and split up the data. 
    while(startPos < record.length()) { 
     pos = record.find(token, startPos); 
     if (pos == string::npos) { 
      break; 
     } 

     results.push_back(record.substr(startPos, pos - startPos)); 
     startPos = pos + token.length(); 
    } 

    // Step: Get the last (or only bit). 
    results.push_back(record.substr(startPos, record.length() - startPos)); 

    // Step: Return the results of the split. 
    return results; 
} 

void printData(vector<string> list) { 
    for(vector<string>::iterator it = list.begin(); it < list.end(); it++) { 
     cout << *it << endl; 
    } 
} 

int main(int argc, char** argv) { 
    string record = ""; 
    string delim = ""; 

    if (argc == 3) { 
     record = argv[1]; 
     delim = argv[2]; 
     printData(split(record,delim)); 
    } else { 
     string record = "comma,delimited,data"; 
     string delim = ","; 
     printData(split(record,delim)); 

     record = "One<--->Two<--->Three<--->Four"; 
     delim = "<--->"; 
     printData(split(record,delim)); 
    } 
} 
ここ
0

あなたのループは正しいことをしていないようです。あなたは文字ごとに歩き回り、各繰り返しの間に1つずつ進んでください。start私は、あなたが実際に現在位置を持つようにしたいと思われる次の区切り文字を見つけ、その結果を現在の位置と区切り文字の間の文字列を追加し、区切りの後、現在位置の文字になるだろう:

for (std::string::size_type start(0); start != s.npos;) 
{ 
    std::string::size_type end(s.find_first_of(delim, start)); 
    tokens.push_back(s.substr(start, end != s.npos? end - start: end)); 
    start = end != s.npos? end + 1: s.npos; 
} 
関連する問題