why there is no find for vector in C++

后端 未结 6 1002
傲寒
傲寒 2021-02-14 03:13

what\'s the alternative?

Should I write by myself?

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-14 03:42

    There is the std::find() algorithm, which performs a linear search over an iterator range, e.g.,

    std::vector v;
    
    // Finds the first element in the vector that has the value 42:
    // If there is no such value, it == v.end()
    std::vector::const_iterator it = std::find(v.begin(), v.end(), 42);
    

    If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin and end iterators to the range of elements in the vector that have that value.

提交回复
热议问题