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

后端 未结 18 2194
滥情空心
滥情空心 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:22

    Here's a function that will work for any Container:

    template <class Container> 
    const bool contains(const Container& container, const typename Container::value_type& element) 
    {
        return std::find(container.begin(), container.end(), element) != container.end();
    }
    

    Note that you can get away with 1 template parameter because you can extract the value_type from the Container. You need the typename because Container::value_type is a dependent name.

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

    (C++17 and above):

    can use std::search also

    This is also useful for searching sequence of elements.

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    template <typename Container>
    bool search_vector(const Container& vec, const Container& searchvec)
    {
        return std::search(vec.begin(), vec.end(), searchvec.begin(), searchvec.end()) != vec.end();
    }
    
    int main()
    {
         std::vector<int> v = {2,4,6,8};
    
         //THIS WORKS. SEARCHING ONLY ONE ELEMENT.
         std::vector<int> searchVector1 = {2};
         if(search_vector(v,searchVector1))
             std::cout<<"searchVector1 found"<<std::endl;
         else
             std::cout<<"searchVector1 not found"<<std::endl;
    
         //THIS WORKS, AS THE ELEMENTS ARE SEQUENTIAL.
         std::vector<int> searchVector2 = {6,8};
         if(search_vector(v,searchVector2))
             std::cout<<"searchVector2 found"<<std::endl;
         else
             std::cout<<"searchVector2 not found"<<std::endl;
    
         //THIS WILL NOT WORK, AS THE ELEMENTS ARE NOT SEQUENTIAL.
         std::vector<int> searchVector3 = {8,6};
         if(search_vector(v,searchVector3))
             std::cout<<"searchVector3 found"<<std::endl;
         else
             std::cout<<"searchVector3 not found"<<std::endl;
    }
    

    Also there is flexibility of passing some search algorithms. Refer here.

    https://en.cppreference.com/w/cpp/algorithm/search

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

    You can use count too. It will return the number of items present in a vector.

    int t=count(vec.begin(),vec.end(),item);
    
    0 讨论(0)
  • 2020-11-22 06:25

    If you wanna find a string in a vector:

        struct isEqual
    {
        isEqual(const std::string& s): m_s(s)
        {}
    
        bool operator()(OIDV* l)
        {
            return l->oid == m_s;
        }
    
        std::string m_s;
    };
    struct OIDV
    {
        string oid;
    //else
    };
    VecOidv::iterator itFind=find_if(vecOidv.begin(),vecOidv.end(),isEqual(szTmp));
    
    0 讨论(0)
  • 2020-11-22 06:26

    In C++11 you can use any_of. For example if it is a vector<string> v; then:

    if (any_of(v.begin(), v.end(), bind(equal_to<string>(), _1, item)))
       do_this();
    else
       do_that();
    

    Alternatively, use a lambda:

    if (any_of(v.begin(), v.end(), [&](const std::string& elem) { return elem == item; }))
       do_this();
    else
       do_that();
    
    0 讨论(0)
  • 2020-11-22 06:27

    Using Newton C++ it's easier, self-documented and faster than with std::find because of return a bool directly.

    bool exists_linear( INPUT_ITERATOR first, INPUT_ITERATOR last, const T& value )
    
    bool exists_binary( INPUT_ITERATOR first, INPUT_ITERATOR last, const T& value )
    

    I think it's obvious what the functions do.

    include <newton/algorithm/algorithm.hpp>
    
    if ( newton::exists_linear(first, last, value) )
       do_this();
    else
       do_that();
    
    0 讨论(0)
提交回复
热议问题