2

私が持っている場合:

const string food[] = {"Burgers", "3", "Fries", "Milkshake"}
string word;
cin >> word;

どうすれば単語と正しい食べ物を比較できますか? むしろ、ユーザーが「フライドポテト」と入力した場合、それを文字列配列と比較するにはどうすればよいですか?

4

2 に答える 2

7

find:

#include <algorithm>
#include <iterator>

auto it = std::find(std::begin(food), std::end(food), word);

if (it != std::end(food))
{
    // found *it
}
else
{
    // not found
}
于 2013-09-11T13:33:24.713 に答える