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

后端 未结 18 2481
滥情空心
滥情空心 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: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 
    
    if ( newton::exists_linear(first, last, value) )
       do_this();
    else
       do_that();
    

提交回复
热议问题