#include <iostream>
#include <map>
using namespace std;
class SomeClass {
public:
SomeClass(const std::string& name): m_name(name) {}
std::string GetName()const {return m_name; }
bool operator <(const SomeClass& rhs) const
{
return m_name < rhs.m_name;
}
private:
std::string m_name;
};
auto print_seq = [](const auto& seq) { for (const auto& itr: seq) {std::cout << itr.second << ":";} std::cout << std::endl;};
int main() {
// your code goes here
std::map<SomeClass*, std::string> pointer_keyed_map;
SomeClass s3("object3");
SomeClass s1("object1");
SomeClass s2("object2");
pointer_keyed_map.insert(std::make_pair(&s1, s1.GetName()));
pointer_keyed_map.insert(std::make_pair(&s2, s2.GetName()));
pointer_keyed_map.insert(std::make_pair(&s3, s3.GetName()));
std::cout << "Pointer based keys: object order" << std::endl;
print_seq(pointer_keyed_map);
std::map<SomeClass, std::string> int_keyed_map;
int_keyed_map.insert(std::make_pair(s3, s3.GetName()));
int_keyed_map.insert(std::make_pair(s1, s1.GetName()));
int_keyed_map.insert(std::make_pair(s2, s2.GetName()));
std::cout << "String based keys: object order" << std::endl;
print_seq(int_keyed_map);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWFwPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgU29tZUNsYXNzIHsKCXB1YmxpYzoKCVNvbWVDbGFzcyhjb25zdCBzdGQ6OnN0cmluZyYgbmFtZSk6IG1fbmFtZShuYW1lKSB7fQoJc3RkOjpzdHJpbmcgR2V0TmFtZSgpY29uc3Qge3JldHVybiBtX25hbWU7IH0KICAgIGJvb2wgb3BlcmF0b3IgPChjb25zdCBTb21lQ2xhc3MmIHJocykgY29uc3QKICAgIHsKICAgICAgICByZXR1cm4gbV9uYW1lIDwgcmhzLm1fbmFtZTsKICAgIH0KCXByaXZhdGU6CglzdGQ6OnN0cmluZyBtX25hbWU7Cn07CgphdXRvIHByaW50X3NlcSAgPSBbXShjb25zdCBhdXRvJiBzZXEpIHsgZm9yIChjb25zdCBhdXRvJiBpdHI6IHNlcSkge3N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