How to use STL binary search operations with pairs ? I also wish to use lower_bound function . Is it possible ?
Asked
Active
Viewed 334 times
1 Answers
2
std::pair
overloads the operator<
thus any algorithm that uses ordering will work with pairs (assuming you've overloaded operator<
for the pair members). The ordering is first by the first
member and then by second
if the first
parts are equal. The lower_bound
function that takes no predicate as argument uses the operator<
for the comparison, thus it will work for pairs too.

Ivaylo Strandjev
- 69,226
- 18
- 123
- 176
-
Can you give sample illustration on how to implement it (lower_bound particularly), i am still not clear with it :) – Maggi Iggam Apr 06 '15 at 14:04
-
1If your question is how to overload the < operator, have a look [here](http://stackoverflow.com/q/4421706/812912) – Ivaylo Strandjev Apr 06 '15 at 14:06
-
Hey i am not able to make it work , pair doesn't has iterators . Can you please provide a snippet on how to use lower_bound function on say pair
please? – Maggi Iggam Apr 06 '15 at 15:03 -
A lower_bound is defined on a collection not an object. Maybe I misunderstood your question - what exactly are you trying to do? – Ivaylo Strandjev Apr 06 '15 at 15:06
-
I'm searching for pairs where (pair.first == foo) how do I state that as a val? – Jorge Barrios Oct 04 '17 at 00:17