I have this question:
I have a std::map
of strings to integers representing list of fruits:
map<string, int> fruits{
{"Apple", 5}, {"Grapefruit", 7}, {"Cherry", 10}, {"Grapes", 16}
};
for (const auto& p : fruits)
cout << p.first << " " << p.second << endl;
cout << endl << endl;
auto it = fruits.begin();
++++it;
using type = std::pair<class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char> > const, int>;
auto it2 = std::lower_bound(fruits.cbegin(), fruits.cend(), type{"Cherry", 10});
// auto it3 = std::lower_bound(fruits.cbegin(), fruits.cend(), pair<string, int>{ "Cherry", 10 });
auto it4 = std::lower_bound(fruits.cbegin(), fruits.cend(), pair<const string, int>{ "Cherry", 10 });
for (auto beg = fruits.cbegin(); beg != it2; ++beg)
cout << beg->first << " " << beg->second << endl;
cout << typeid(*it).name() << endl;
So my problem was How to pass the third parameter to std::lower_bound
explicitly?
Because after getting help from
typeid
I've noticed that the pair'sfirst
isconst
is this because the keys areconstants
?Also what happen if I pass the value for a given key that is not matching the key in the container. e.g: the element with key
"Cherry"
has a value10
so whylower_bound
works correctly if I pass to it invalidvalue
with the key likepair{"Cherry", 345}
?Is the value of the pair passed to
lower_bound
arbitrary?