2011-10-19 13 views
0

私はC++ Primerを読んで、その演習に取り組んでいます。ユーザーの入力を受け取り、スペースで区切ってください。' 'だから私は2つの解決策を考え出す。C++のスペースでユーザー入力を区切るには、より良い解決策はありますか?

まず解決策:

vector<string> vector1; 
string input; 
string temp = ""; // temperary hold each word value in input. 

string x1 = ""; 
char x2 = 'b'; 

x1 += x2; 

cout << x1 << endl; 

getline(cin, input); 

input += " "; 

for (string::size_type index = 0; index != input.size(); index++) 
{ 
    if (!isspace(input[index])) 
    { 
     temp += input[index]; 
    } 
    else 
    { 
     if (temp.size() > 0) 
     { 
      vector1.push_back(temp); 
      temp = ""; 
     } 
    } 
} 

第二の溶液

vector<string> vector1; 
string input; 
string temp = ""; // temperary hold each word value in input. 

string x1 = ""; 
char x2 = 'b'; 

x1 += x2; 

cout << x1 << endl; 

getline(cin, input); 

//input += " "; 

for (string::size_type index = 0; index != input.size(); index++) 
{ 
    if (!isspace(input[index])) 
    { 
     temp += input[index]; 
    } 
    else 
    { 
     if (temp.size() > 0) 
     { 
      vector1.push_back(temp); 
      temp = ""; 
     } 
    } 
} 

if (!temp.empty()) 
{ 
    vector1.push_back(temp); 
} 

それらの間の差は、第一の溶液は、第二の溶液のチェック、私は追加しないことをしながら、ユーザの入力にスペースを追加です最後の単語かどうか。どの問題がこの問題の解決策であるかを知りたいですか?

もっと良い解決策がある場合は、教えてください。

+1

は、あなたはそれがやっているかを説明してコードにいくつかのコメントを追加する必要があります。 –

+0

あなたの2つのソリューションはほぼ同じです... – AJG85

答えて

4

私はこの記述します。

std::vector<std::string> data; 

std::copy(std::istream_iterator< std::string>(std::cin), 
      std::istream_iterator< std::string>(), 
      std::back_inserter(data)); 

それは私がstd::copyは、入力ストリーム(すなわちstd::cin)からではなく、std::stringstreamから直接読み取ることができていることを除いて、K-BALLOの答え@とほぼ同じであるが。

デモ:http://www.ideone.com/f0Gtc

-

それとも、完全std::copyを避けて、ベクトルのコンストラクタを使用することができます

std::vector<std::string> data(std::istream_iterator<std::string>(std::cin), 
           std::istream_iterator<std::string>()); 

をそして、あなたが行われています!デモ:http://www.ideone.com/Szfes

あなたはそれが困難読むことを見つけた場合は、代わりにこれを使用する:

std::istream_iterator<std::string> begin(std::cin), end; 
std::vector<std::string> data(begin, end); 

デモ:http://www.ideone.com/PDcud

+0

私は最近、一度に1行ずつ読み上げるプログラムをたくさん行ってきましたが、OPのコードもそうしているので、私はその考え方にとどまっていました。あなたがコード改行を気にしなければ、コードは(はるかに)速くなります。 –

+0

@MooingDuck:私はそれをさらに編集して編集しました。 – Nawaz

+1

+1のセクシーファクター – AJG85

0

簡単に文字列区切り値にストリームからの入力を分割し、でベクターにそれらを挿入することができる:std::istream_iterator<std::string>対が一度std::stringを抽出することによりinput_streamを反復します

string input; 

... get the input, perhaps with getline(cin, input); ... 

stringstream input_stream(input); 
vector<string> vector1; 

std::copy(
    (std::istream_iterator<std::string>(input_stream)), std::istream_iterator<std::string>() 
    , std::back_inserter(vector1) 
); 

(A文字列は空白文字が見つかるまで読み込まれます)。 std::back_inserterは、それぞれの文字列のvectorpush_backを呼び出します。

+0

なぜ 'stringstream'が必要ですか?私のソリューションで行ったように、 'std :: cin'から直接読むことができないのはなぜですか?それとも、私は行方不明のものがありますか? – Nawaz

+0

@Nawaz:OPは 'std :: cin'から1行だけを分割したいと思うので、 –

1

C++では、スペースで区切られた値を読み込むことは非常に簡単で、言語に組み込まれています。私は間違っているかもしれませんが、あなたが過度に複雑なものであるように見えます。

std::string line; 
if (std::getline(std::cin, line)) { 
    std::stringstream ss(line); 
    std::vector<std::string> inputs_on_this_line(
       std::istream_iterator<std::string>(ss) 
      , std::istream_iterator<std::string>() ); 
    //do stuff with the strings on this line. 
} 
+0

awですが、過密化はそのような素晴らしい単語です。 –

+0

さらに簡素化することができます。私の解決策を見てください。 – Nawaz

+1

@OP:改行を気にする人は、このアイディアを使用してください。入力の_ALL_が必要な場合は、Nawazのバージョンを使用します。彼の方が速いです。 –

関連する問題