ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • STL map 의 정리
    C++ 2014. 2. 20. 22:51

    Map은 4개의 template parameter가 필요하다.

    1. Key Type
    2. Data Value Type
    3. Key compare type
    4. Data allocation type

     

    explicit map (const key_compare& comp = key_compare(),

    const allocator_type& alloc = allocator_type());

     

    template <class InputIterator>
    map (InputIterator first, InputIterator last, const key_compare& comp = key_compare(),

    const allocator_type& alloc = allocator_type());

     

    map (const map& x);

     

    Compare type을 지정하기 위해서는 operator < 를 따로 override 해야 한다. Default로 < 는 less than을 사용한다.

    Example

    class SimpleMap

    {

    private:

        int mVal;

    public:

        SimpleMap(int val=0);

        virtual ~SimpleMap(void);

        const int& get();

        void set(const int& val);

    };

    1. Data의 입력은

      By Insert ---- Key, Data 항상 이렇게 pair로 입력을 한다.
      1) make_pair()를 사용한다.
      2) pair container를 사용한다.

      3) operator[]를 이용 직접 입력한다. 이때는 객체가 생성이 되는 것에 유의하고 그래서 가장 효울이 나쁘다.

       

    2. Insert Error Return
      1. 2)의 경우에는 return 값이 있는데 pair로 <pair>( it iterator , bool error) 가 return 된다.
        중복키가 입력되면 error를 return

        3)의 경우에는 return이 없이 중복 키는 overwrite가 된다.

        map<int, SimpleMap> dataMap;

     

    //    C++98

    //    pair<map<int,SimpleMap>::iterator,bool> ret =

    // C++11

        auto ret =

            dataMap.insert(make_pair(1, SimpleMap(100)));

        if (ret.second)

            cout << "1) insert success" << endl;

        else

            cout << "1) insert fail" << endl;

     

    //    ret = dataMap.insert({(2, SimpleMap(200)}); // C++11 initializer_list

        ret = dataMap.insert(pair<int, SimpleMap>(2, SimpleMap(200)));

        if (ret.second)

            cout << "2) insert success" << endl;

        else

            cout << "2) insert fail" << endl;

     

        // insert a same key, and return error

        ret = dataMap.insert(pair<int, SimpleMap>(2, SimpleMap(200)));

        if (ret.second)

            cout << "3) insert success" << endl;

        else

            cout << "3) insert fail" << endl;

     

        map<int,SimpleMap>::iterator it = dataMap.begin();

        // no return

        dataMap.insert(it, pair<int, SimpleMap>(3, SimpleMap(300))); // most efficiency

        dataMap.insert(it, pair<int, SimpleMap>(4, SimpleMap(400))); // no max efficiency

     

        dataMap[5] = SimpleMap(500); // create a object, least efficiency

        dataMap[1] = SimpleMap(600); // overwrite, no error return

     

        map<int, SimpleMap> anotherMap;

     

    // find() 통한 Insert 주목

        anotherMap.insert(dataMap.begin(), dataMap.find(3));

     

        for (map<int,SimpleMap>::iterator it=dataMap.begin();it!=dataMap.end();++it)

            cout << it->first << " " << it->second.get() << endl;

     

        for (auto it=anotherMap.begin();it!=anotherMap.end();++it)

            cout << it->first << " " << it->second.get() << endl;

     

        for (auto it=anotherMap.begin();it!=anotherMap.end();++it)

            cout << (*it).first << " " << (*it).second.get() << endl;

     

        for (auto& p:anotherMap)

            cout << p.first << " " << p.second.get() << endl;

     

        dataMap[2].set(700);

        

        it = dataMap.find(3);

        if (it!=dataMap.end())

            it->second.set(800);

     

        dataMap.erase(4);

     

        for (auto& i:dataMap)

            cout << i.first << " " << dataMap[i.first].get() << endl;

     

    }

    'C++' 카테고리의 다른 글

    Bitset 의 예  (0) 2014.02.22
    MultiMap  (0) 2014.02.22
    Packet Queue Example Code  (0) 2014.02.20
    Round Robin Queue를 이용한 Process Scheduler  (0) 2014.02.19
    Vector constructor and descructor  (0) 2014.02.17
Designed by Tistory.