1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <string>
#include <map>
#include <functional>
int main() {
struct ComparisonStringLength {
bool operator()(const std::string& string_one, const std::string& string_two) const {
return (string_one.size() < string_two.size());
}
};
//Uses std::less comparison object by default
std::map<std::string, int/*, std::less<std::string>*/> map_less = { {"also", 1}, {"beautiful", 1}, {"car", 1} };
std::map<std::string, int, std::greater<std::string>> map_greater = { { "also", 1 },{ "beautiful", 1 },{ "car", 1 } };
std::map<std::string, int, ComparisonStringLength> map_longer = { { "also", 1 },{ "beautiful", 1 },{ "car", 1 } };
std::cout << "Map (std::less):" << std::endl;
for (const auto& it : map_less) {
std::cout << it.first << std::endl;
}
std::cout << std::endl;
std::cout << "Map (std::greater):" << std::endl;
for (const auto& it : map_greater) {
std::cout << it.first << std::endl;
}
std::cout << std::endl;
std::cout << "Map (ComparisonStringLength):" << std::endl;
for (const auto& it : map_longer) {
std::cout << it.first << std::endl;
}
std::cout << std::endl;
std::cin.ignore();
return 0;
}
|
Map (std::less):
also
beautiful
car
Map (std::greater):
car
beautiful
also
Map (ComparisonStringLength):
car
also
beautiful |