LoginSignup
7
7

More than 5 years have passed since last update.

std::multimap memo

Last updated at Posted at 2014-02-22

いつも忘れるので使いかたメモ

#include <iostream>
#include <map>
#include <string>

using namespace std;

void dump(multimap<float, string>::iterator begin, multimap<float, string>::iterator end)
{
    multimap<float, string>::iterator it = begin;

    cout << "---" << endl;
    while (it != end)
    {
        cout << it->first << ": " << it->second << endl;
        it++;
    }
    cout << "---" << endl << endl;
}

void dump(multimap<float, string>& m)
{
    dump(m.begin(), m.end());
}

int main(int argc, char *argv[]) {

    multimap<float, string> m;

    // 要素追加
    m.insert(make_pair(0, "test0"));
    m.insert(make_pair(1, "test1"));
    m.insert(make_pair(2, "test2"));
    dump(m);
    //  ---
    //  0: test0
    //  1: test1
    //  2: test2
    //  ---


    // 重複した要素があってもOK
    m.insert(make_pair(0, "doubled0"));
    m.insert(make_pair(1, "doubled1"));
    dump(m);
    //  ---
    //  0: test0
    //  0: doubled
    //  1: test1
    //  2: test2
    //  ---


    // キーを指定して要素を取得
    {
        std::pair<multimap<float, string>::iterator, multimap<float, string>::iterator> range = m.equal_range(1);
        dump(range.first, range.second);
    }
    //  ---
    //  1: test1
    //  1: doubled1
    //  ---



    // 範囲指定で要素を取得 [0, 1)
    {
        multimap<float, string>::iterator begin = m.lower_bound(0);
        multimap<float, string>::iterator end = m.lower_bound(1);
        dump(begin, end);
    }
    //  ---
    //  0: test0
    //  0: doubled0
    //  ---


    // 範囲指定で要素を取得 [0, 1]
    {
        multimap<float, string>::iterator begin = m.lower_bound(0);
        multimap<float, string>::iterator end = m.upper_bound(1);
        dump(begin, end);
    }
    //  ---
    //  0: test0
    //  0: doubled0
    //  1: test1
    //  1: doubled1
    //  ---


    // 要素の削除
    m.erase(0);
    dump(m);
    //  ---
    //  1: test1
    //  1: doubled1
    //  2: test2
    //  ---


    // 狙った要素だけ取り出し
    {
        int n = m.count(1);
        multimap<float, string>::iterator it = m.find(1);
        for (int i = 0; i < n; i++)
        {
            if (it->second == "doubled1")
            {
                cout << "found: at index " << i << endl;
            }
            it++;
        }

        cout << endl;
    }


    // 狙った要素だけ削除
    {
        string to_remove_string = "doubled1";

        int n = m.count(1);
        multimap<float, string>::iterator it = m.find(1);
        for (int i = 0; i < n; i++)
        {
            if (it->second == to_remove_string)
            {
                m.erase(it);
            }
            else
                it++;
        }
    }
    dump(m);
    //  ---
    //  1: test1
    //  2: test2
    //  ---

}
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7