stringstream
を使用して、文章から単語を抽出します。std :: coutが出力に改行または改行を追加するのはなぜですか?
単語ごとに改行が追加されていますが、その理由はわかりません。あなたはできる?あなたの助けにThx。 Keith:^)
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
int main() {
int wordCount(9);
std::vector<std::string> v(wordCount);
std::vector<std::string>::iterator it;
std::string tStr;
std::string myStr = "the quick fox jumped over the lazy brown dogs";
std::stringstream ss(myStr);
std::cout << "Sentence broken into vector:" << std::endl;
for (int i=0; i<wordCount; ++i) {
ss >> tStr;
v.push_back(tStr);
}
for (it=v.begin(); it != v.end(); ++it)
std::cout << *it << std::endl;
std::cout << std::endl;
return 0;
}
コンパイル、実行、および出力。余分な改行に注意してください。あなたはwordCount
空のエントリを作成しstd::vector<std::string> v(wordCount);
あなたはこの行を使用してベクトルを作成し
pickledEgg $ g++ -std=c++11 -g -Og -o io2 io2.cpp
pickledEgg $ ./io2
Sentence broken into vector:
the
quick
fox
jumped
over
the
lazy
brown
dogs
これは、デバッガを使用するのに最適なタイミングです。 – chris
あなたはそのコードのどこでも 'std :: cin'を使用していません... – qxz
9つの空文字列でベクトルを作成し、最後に他の文字列をプッシュしました...出力は正確に一致します –