Reputation: 967
How is Comp{}
in std::equal_range
triggering the Compare operation? Is Comp{}
indicating a R-Value being created?
#include <algorithm>
#include <vector>
#include <iostream>
struct S
{
int number;
char name;
// note: name is ignored by this comparison operator
bool operator< ( const S& s ) const { return number < s.number; }
};
int main()
{
// note: not ordered, only partitioned w.r.t. S defined below
const std::vector<S> vec = { {1,'A'}, {2,'B'}, {2,'C'}, {2,'D'}, {4,'G'}, {3,'F'} };
std::cout << "\n" "Using heterogeneous comparison: ";
struct Comp
{
bool operator() ( const S& s, int i ) const { return s.number < i; }
bool operator() ( int i, const S& s ) const { return i < s.number; }
};
const auto p2 = std::equal_range(vec.begin(),vec.end(), 2, Comp{});
for ( auto i = p2.first; i != p2.second; ++i )
std::cout << i->name << ' ';
}
Upvotes: 2
Views: 110