How to find out if an item is present in a std::vector?

后端 未结 18 2224
滥情空心
滥情空心 2020-11-22 05:31

All I want to do is to check whether an element exists in the vector or not, so I can deal with each case.

if ( item_present )
   do_this();
else
   do_that(         


        
相关标签:
18条回答
  • 2020-11-22 06:04

    Use find from the algorithm header of stl.I've illustrated its use with int type. You can use any type you like as long as you can compare for equality (overload == if you need to for your custom class).

    #include <algorithm>
    #include <vector>
    
    using namespace std;
    int main()
    {   
        typedef vector<int> IntContainer;
        typedef IntContainer::iterator IntIterator;
    
        IntContainer vw;
    
        //...
    
        // find 5
        IntIterator i = find(vw.begin(), vw.end(), 5);
    
        if (i != vw.end()) {
            // found it
        } else {
            // doesn't exist
        }
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 06:04

    Bear in mind that, if you're going to be doing a lot of lookups, there are STL containers that are better for that. I don't know what your application is, but associative containers like std::map may be worth considering.

    std::vector is the container of choice unless you have a reason for another, and lookups by value can be such a reason.

    0 讨论(0)
  • 2020-11-22 06:04

    I've personally used templates of late to handle multiple types of containers at once rather than deal only with vectors. I found a similar example online (can't remember where) so credit goes to whoever I've pilfered this from. This particular pattern seems to handle raw arrays as well.

    template <typename Container, typename T = typename std::decay<decltype(*std::begin(std::declval<Container>()))>::type>
    bool contains(Container && c, T v)
    {
        return std::find(std::begin(c), std::end(c), v) != std::end(c);
    }
    
    0 讨论(0)
  • 2020-11-22 06:05

    You can try this code:

    #include <algorithm>
    #include <vector>
    
    // You can use class, struct or primitive data type for Item
    struct Item {
        //Some fields
    };
    typedef std::vector<Item> ItemVector;
    typedef ItemVector::iterator ItemIterator;
    //...
    ItemVector vtItem;
    //... (init data for vtItem)
    Item itemToFind;
    //...
    
    ItemIterator itemItr;
    itemItr = std::find(vtItem.begin(), vtItem.end(), itemToFind);
    if (itemItr != vtItem.end()) {
        // Item found
        // doThis()
    }
    else {
        // Item not found
        // doThat()
    }
    
    0 讨论(0)
  • 2020-11-22 06:05
    template <typename T> bool IsInVector(const T & what, const std::vector<T> & vec)
    {
        return std::find(vec.begin(),vec.end(),what)!=vec.end();
    }
    
    0 讨论(0)
  • 2020-11-22 06:18

    You can use the find function, found in the std namespace, ie std::find. You pass the std::find function the begin and end iterator from the vector you want to search, along with the element you're looking for and compare the resulting iterator to the end of the vector to see if they match or not.

    std::find(vector.begin(), vector.end(), item) != vector.end()
    

    You're also able to dereference that iterator and use it as normal, like any other iterator.

    0 讨论(0)
提交回复
热议问题