2012-05-02 5 views
0

私は以下のようなデータセットの文字列ベクトルを持っています。文字列内の異なるデータを区切ります

vector<string> result; 

<index> | <Name> | <email> | <status> 

    1|duleep|[email protected]|0 
    2|dasun|[email protected]|0 
    3|sampath|[email protected]|1 
    4|Nuwan|[email protected]|0 

今私は別のベクトルデータを取得したい(名前、インデックス、ステータス)この使用してC++(どのように私は、文字列配列に変換することができます[4] [4]?)

+0

あなた 'data'は'どのようなものを見てclass'ん?演算子>>() 'をオーバーロードしましたか? – Johnsyweb

+0

文字列をトークン化したい場合は、C++の文字列を探すことができます – Raghuram

+0

@Raghuramそれは私の問題のための最良の解決策ですか?配列へのアクセスのような単純なアクセス方法はありますか? – user881703

答えて

1
を行うための最善の方法を提案してくださいここ

は(あなたがブーストを使用しない場合は申し訳ありませんそして、)私はboost::tokenizerを使用して思い付いた例です。

#include <iostream> 
#include <boost/tokenizer.hpp> 
#include <string> 
#include <vector> 
int main() 
{ 
    std::vector<std::string> v; 
    v.push_back("1|duleep|[email protected]|0"); 
    v.push_back("2|dasun|[email protected]|0"); 
    v.push_back("3|sampath|[email protected]|1"); 
    v.push_back("4|Nuwan|[email protected]|0"); 

    boost::char_separator<char> sep("|"); 
    std::vector<boost::tokenizer<boost::char_separator<char>>> tokens; 
    for (auto& s : v) 
    { 
    tokens.push_back({s, sep}); 
    } 
} 

あなたがstd::string array[4][4]を使用したい場合は、単にトークンを反復処理し、あなたに割り当てますアレイ。ここで

は、ブーストなしの別の方法です:

for (auto& s : v) 
    { 
    std::stringstream ss(s); 
    std::string token; 
    while (std::getline(ss, token, '|')) 
    { 
     // Put token into your array here 
    } 
    } 
+0

あなたのフィードバックをありがとう文字列配列を取得する方法はありますか?(3partyライブラリを使用しないでください) – user881703

+1

@ user881703:私は私の答えで別の方法を追加しました(しかし、私はすべてのコードを提供しませんでした)。 –

関連する問題