C++ examples for STL:map
Creating a map to store user objects
#include <cinttypes> #include <functional> #include <iostream> #include <map> #include <string> using namespace std; class MapObject/*from ww w . j av a 2 s .c o m*/ { private: string m_Name; public: MapObject(const string& name) : m_Name{ name } { } const string& GetName() const { return m_Name; } }; int main(int argv, char* argc[]) { map<int32_t, MapObject, greater<int32_t>> myMap { pair<int32_t, MapObject>(6, MapObject("Six")), pair<int32_t, MapObject>(3, MapObject("Three")), pair<int32_t, MapObject>(4, MapObject("Four")), pair<int32_t, MapObject>(1, MapObject("One")), pair<int32_t, MapObject>(2, MapObject("Two")) }; for (auto&& number : myMap) { cout << number.second.GetName() << endl; } cout << endl; myMap.emplace(pair<int32_t, MapObject>(5, MapObject("Five"))); for (auto&& number : myMap) { cout << number.second.GetName() << endl; } cout << endl; auto iter = myMap.find(3); if (iter != myMap.end()) { cout << "Found: " << iter->second.GetName() << endl; } return 0; }