I wrote a program for processing and outputting words, with the addition of undesirable words to it, which, if entered, would have written BEEP.

{ vector<string> disliked = {"Broccoli", "Peperoni"}; //Вектор нежелательных слов string word; cout << "Enter the word\n"; while (cin >> word) { if (word == disliked[0, 1]) //Проверка слова на наличие в векторе cout << "BEEEP!!" << endl; else cout << "You entered " << word << endl; } keep_window_open(); return 0; } 

This is what I did. But, as you can see, when you increase the vector, I will have to add more numbers to the if (word == disliked[0, 1]) . How would you like the program to automatically add words to a vector when adding words to a vector?

  • Well, for one question, is it possible in some way to make the program output Cyrillic? - niki4iko

2 answers 2

Do you know what you actually wrote here?

 if (word == disliked[0, 1]) 

disliked[1] for equality with disliked[1] . Because a comma is an operator for sequential execution of instructions; as a result, the last value is taken. You got two instructions in square brackets - 0 (which is ignored, and about which the compiler should have issued a warning) and 1 , which becomes the result ...

And that's what you wanted to write :) -

more briefly, using std::find from <algorithm> :

 while (cout << "Enter the word: ", cin >> word) { // Проверка, есть ли слово word в векторе if (find(disliked.begin(), disliked.end(),word) != disliked.end()) cout << "BEEEP!!" << endl; else cout << "You entered " << word << endl; } 

Please note that here is while (cout << "Enter the word: ", cin >> word) - I used the comma-operator mentioned above to ask for a word for each word, but not once for all the words entered.

It is possible in more detail, so that it would be clearer to you - just go through the elements using a loop:

 while (cout << "Enter the word: ", cin >> word) { bool found = false; // Флаг, что слово найдено for(size_t i = 0; i < disliked.size(); ++i) // Проход по всем словам if (word == disliked[i]) // Если найдено { found = true; // выставляем флаг и break; // покидаем цикл (дальше искать не смысла) } if (found) // Было найдено cout << "BEEEP!!" << endl; else cout << "You entered " << word << endl; } 

About Russian letters - see the answers to this question: Russian language in the console

  • And at first glance, I decided that this 0,1 will not compile at all. Still, this comma is rarely seen. - Mikhailo
  • @Mikhailo Sometimes she is very helpful. But, of course, remembering only what you use is a common human trait :) - Harry

To what has already been written by @Harry I will add that for such purposes it is better to use std::set or std::unordered_set . The search in std::vector is performed in linear time ( O (n) ), std::set is logarithmic ( O (log (n)) ), std::unordered_set is a depreciated constant ( O (1) ).

 #include <set> //... std::set<std::string> disliked = {"Broccoli", "Peperoni"}; //... if(disliked.count(word)){ // Если найдено //... }