I've got a node struct

```
struct Node{CString text, int id;};
```

in a sorted vector.

I'm wondering if there's a function in algorithm that will do a binary search of the vector and find an element.

Skip to content
# C++ – What function in the std library is there to binary search a vector and find an element

###### Related Question

binary-searchc++stdvector

I've got a node struct

```
struct Node{CString text, int id;};
```

in a sorted vector.

I'm wondering if there's a function in algorithm that will do a binary search of the vector and find an element.

- C++ – How to find out if an item is present in a std::vector
- C++ – How to erase an element from std::vector<> by index
- C++ – the difference between const int*, const int * const, and int const *
- C++ – the easiest way to initialize a std::vector with hardcoded elements
- C++ – Why do we need virtual functions in C++
- C++ – the copy-and-swap idiom
- C++ – What are the basic rules and idioms for operator overloading
- C++ – Image Processing: Algorithm Improvement for ‘Coca-Cola Can’ Recognition

## Best Solution

`std::binary_search()`

will tell you if a value exists in the container.`std::lower_bound()/std::upper_bound()`

will return an iterator to the first/last occurrence of a value.Your objects need to implement

`operator<`

for these algorithms to work.