私はカンマ区切りの単語を含む文字ベクトルを持っています。 単語でテキストを区切り、それらの単語をリストに追加する必要があります。おかげさまで <char>をdelimeterを使って文字列に分割する方法
vector<char> text;
list<string> words;
私はカンマ区切りの単語を含む文字ベクトルを持っています。 単語でテキストを区切り、それらの単語をリストに追加する必要があります。おかげさまで <char>をdelimeterを使って文字列に分割する方法
vector<char> text;
list<string> words;
行く方法を確認してみてください:
while ((stop=std::find(start, text.end(), ',')) != text.end()) {
words.push_back(std::string(start, stop));
start = stop+1;
}
words.push_back(std::string(start, text.end()));
編集:とし要件が少し奇妙に思えることを指摘しておかなければなりません。なぜ、std::vector<char>
で始まっていますか? std::string
がより一般的になります。
vector<char> text = ...;
list<string> words;
ostringstream s;
for (auto c : text)
if (c == ',')
{
words.push_back(s.str());
s.str("");
}
else
s.put(c);
words.push_back(s.str());
ostringstreamの使用中に不完全な型が許可されていません。 – jjjojjjooo
@jjjojjooo: '
この単純な擬似コードをコーディングし、それは私が、私はそれをこのような何かと思う
string tmp;
for i = 0 to text.size
if text[i] != ','
insert text[i] to tmp via push_back
else add tmp to words via push_back and clear out tmp
うん、今はうまくいきます。 – jjjojjjooo