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.
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.
Yes, there's a function called "binary_search" std::binary_search
You give it first, last and a value or a predicate.
See here for a sample
Combine that with Martin York's operator== and you should be OK (alternatively you can write a predicate functor
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.
Rather than a sorted vector<Node>
Why not use a map. This is a sorted container. So any search done on this via std::find() automatically has the same properties as a binary search.