私は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);
}
それらの間の差は、第一の溶液は、第二の溶液のチェック、私は追加しないことをしながら、ユーザの入力にスペースを追加です最後の単語かどうか。どの問題がこの問題の解決策であるかを知りたいですか?
もっと良い解決策がある場合は、教えてください。
は、あなたはそれがやっているかを説明してコードにいくつかのコメントを追加する必要があります。 –
あなたの2つのソリューションはほぼ同じです... – AJG85