开发者

How do I check if a element is not present inside of a vector?

开发者 https://www.devze.com 2023-01-23 06:34 出处:网络
This compiles and runs ok on Visual C++ 2010 Express but it only checks against the [2] element: \"Fish\".

This compiles and runs ok on Visual C++ 2010 Express but it only checks against the [2] element: "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 do I make it check against ALL vector elements without typing:

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

? Any other advices of how to make it better or more开发者_JAVA百科 elegant?


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 works 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" << " ";
}


C++0x introduces three algorithms that you might want to check out: all_of, any_of and 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] << " ";
    }
}

But as Alexey already pointed out, in this specific case, you are probably better off with a std::set. You could also do a binary_searchon the vector, but then you have to make sure its sorted.

(Also note that I changed the loop counter type from int to vector<string>::size_type.)


Well you can simply use std::find to search for the word in the vector. But in general, vectors aren't meant for this sort of random-access lookup. You might want to consider using an std::set to store the foods you don't like.

Then you can just say:

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;

Also, consider using an alternative expletive to "BLEEP".


While other methods including set or std::find are actually very good and fast,
you should be able to understand how to make it by yourself.
If you want to check all elements in words against all elements in disliked you actually need another for loop.

   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" << " ";
    }

This is basically the code you will call using std::find. Note that the find method of std::set use a different approach often implemented using red-black tree which is much more efficient.


You basically want to check are the all elements the same. The best idea is to use set. If you need vector for something else the fastest way is to sort the vector, go through it and check if any subsequent elements are same (O(n log(n) + n)). If your vectors aren't big the answer with 2 for loops will do the job (O(n^2)).

0

精彩评论

暂无评论...
验证码 换一张
取 消