C++ Multimap运用实例

C++ Multimap运用实例

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

using namespace std;

int main()
{
    multimap<string, string> dict1;
    dict1.insert({ { "day","Tag" }, { "strange","fremd" }, { "car","Auto" },
    { "smart","elegant" }, { "trait","Merkmal" }, {"strange","seltsam"},
    { "smart","raffiniert" }, { "smart","klug" }, {"clever","raffiniert"} });

    //print all element
    cout.setf(ios::left,ios::adjustfield);
    cout << "" << setw(10) << "english german " << endl;
    cout << setfill('-') << setw(20) << "  " << setfill(' ') << endl;

    for (const auto& elem1:dict1)
    {
        cout << "  " << setw(10) << " " << elem1.first << "  " << elem1.second << endl;
    }
    cout << endl;


    string word1("smart");
    cout << "word1:" << word1 << endl;
    for (auto pos1 = dict1.lower_bound(word1);pos1 != dict1.upper_bound(word1);++pos1)
    {
        cout << "" << pos1->first << "   " << pos1->second << endl;
    }
    cout << endl;

    string word2("raffiniert");
    cout << "word2:" << word2 << endl;
    for (const auto& elem1:dict1)
    {
        if (elem1.second == word2)
        {
            cout << elem1.first << "  " << elem1.second << endl;
        }
    }

    system("pause");
    return 0;
}

english german
------------------
car Auto
clever raffiniert
day Tag
smart elegant
smart raffiniert
smart klug
strange fremd
strange seltsam
trait Merkmal

word1:smart
smart elegant
smart raffiniert
smart klug

word2:raffiniert
clever raffiniert
smart raffiniert
请按任意键继续. . .

代码参考:C++标准库(第2版)

原文地址:https://www.cnblogs.com/herd/p/12069769.html