I am trying to learn how to use std::map with functions. I am a bit confused about how std::map::find
works in this regard. Here is a simple code where I use a Lambda function along with map.
auto Lambda = [](const int&a, const int& b) {
cout << "\n Inside lambda \n";
return a < b;
};
std::map<int, int, decltype(Lambda)> expt(Lambda);
expt[1] = 2;
expt[10] = 12;
auto search1 = expt.find(1);
auto search10 = expt.find(10);
if(search1 != expt.end()) {
std::cout << "Found " << search1->first << " " << search1->second << '\n';
}
else {
std::cout << "Not found\n";
}
Here is the output that I get:
Inside lambda
Inside lambda
Inside lambda
Inside lambda
Inside lambda
Inside lambda
Inside lambda
Found 1 2
I am a bit confused about how find
actually works in this case. Why do I get 7 calls of the lambda function even though I just have 2 keys in my map?
Aucun commentaire:
Enregistrer un commentaire