[태그:] std

  • std::map

    #include <map>
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    template<typename F, typename S>
    class value_equal
    {
    private:
    S second;
    
    public:
    value_equal(const S& s) : second(s){}
    bool operator() (pair<const F, S> elem)
    { return elem.second == second; }
    };
    
    typedef map<int, string> isMap;
    typedef isMap::value_type isValType;
    typedef isMap::iterator isMapItor;
    
    void main()
    {
    isMap c;
    
    c.insert(isValType(100, "one Hundred"));
    c.insert(isValType(3, "Three"));
    c.insert(isValType(150, "one Hundred Fifty"));
    c.insert(isValType(99, "Ninety Nine"));
    
    for(isMapItor itor = c.begin(); itor != c.end(); ++itor)
    cout << "Key = " << (*itor).first << ", Value = " << (*itor).second << endl;
    
    cout << "Key 3 displays value " << c[3].c_str() << endl;
    
    c[123] = "One Hundred Twenty Three";
    
    isMapItor pos = c.find(123);
    
    if(pos != c.end())
    c.erase(pos);
    
    pos = find_if(c.begin(), c.end(), value_equal<int, string>("Ninety Nine"));
    
    if(pos != c.end())
    c.erase(pos);
    
    for(isMapItor itor = c.begin(); itor != c.end();)
    {
    if(itor->second == "Three")
    c.erase(itor++);
    else
    ++itor;
    }
    
    cout << endl;
    for(isMapItor itor = c.begin(); itor != c.end(); ++itor)
    cout << "Key = " << (*itor).first << ", Value = " << (*itor).second << endl;
    
    }