How can I use std::binary_search using just a key?
问题 I have some data that is stored in a sorted vector. This vector is sorted by some key. I know the STL has an algorithm for checking if an element is in this sorted list. This means I can write something like this: struct MyData { int key; OtherData data; }; struct MyComparator { bool operator()( const MyData & d1, const MyData & d2 ) const { return d1.key < d2.key; } }; bool isKeyInVector( int key, const std::vector<MyData> &v ) { MyData thingToSearchFor; thingToSearchFor.key = key; return