How to check if an element is inside a vector?

This one compiles and works fine on Visual C ++ 2010 Express, but it only checks for element [2]: "Fish".

int main()
    {
        vector<string> words;
        string temp;
        vector<string> disliked(3);
        disliked[0] = "Broccoli";
        disliked[1] = "Mushrooms";
        disliked[2] = "Fish";
        while (cin >> temp)
            words.push_back(temp);
        cout << "Number of words: " << words.size() << endl;
        for (int i=0; i<words.size(); ++i) {
            if (words[i]!=disliked[2])
                cout << words[i] << " ";
            else cout << "BLEEP" << " ";
        }
        cout << endl;
        keep_window_open();
        return 0;
    }

How to make it check ALL elements of the vector without input:

if (words[i]!=disliked[0] && words[i]!=disliked[1] && words[i]!=disliked[2])

? Any other tips on how to make it better or more elegant?

+3
source share
5 answers
if (std::find(disliked.begin(), disliked.end(), words[i]) == disliked.end()) {
   cout << words[i] << " ";
} else {
   cout << "BLEEP" << " ";
}

If you replace std::vector<string> disliked(3);with std::set<string> disliked;, it will work faster.

std::set<string> disliked;
disliked.insert("Broccoli");
disliked.insert("Mushrooms");
disliked.insert("Fish");
//....

if (disliked.find(words[i]) == disliked.end()) {
   cout << words[i] << " ";
} else {
   cout << "BLEEP" << " ";
}
+9
source

C ++ 0x introduces three algorithms, you can check: all_of, any_ofand none_of.

#include <algorithm>
#include <functional>

for (vector<string>::size_type i = 0; i < words.size(); ++i)
{
    if (any_of(disliked.begin(),
               disliked.end(),
               bind2nd(equal_to<string>(), words[i])))
    {
        cout << "BLEEP" << " ";
    }
    else
    {
        cout << words[i] << " ";
    }
}

, , , , std::set. binary_search , .

( , int vector<string>::size_type.)

+2

, std::find . , . std::set , .

:

std::set<std::string> dislike;
dislike.insert("Broccoli");
dislike.insert("Mushrooms");
dislike.insert("Fish");

...

if (dislike.find("whatever") != dislike.end()) std::cout << "BLEEP" << std::endl;

, "BLEEP".

+1

, set std:: find, ,
, .
words disliked, for.

   for (std::size_t i = 0; i < words.size(); ++i) {
     bool found = false;
     for (std::size_t j = 0; j < disliked.size(); ++j) {
        if (words[i] == disliked[j]) {
          found = true;
          break;
        }
      if (not found)    
        cout << words[i] << " ";
      else
        cout << "BLEEP" << " ";
    }

, , std::find. , find std::set , - , .

+1

Basically you want to check if they are all the same. The best idea is to use set. If you need a vector for something else, the quickest way is to sort the vector, go through it and check if any subsequent elements are the same (O (n log (n) + n)). If your vectors are small, the answer from 2 for loops will do the job (O (n ^ 2)).

0
source

Source: https://habr.com/ru/post/1772240/


All Articles