C++ Multimap运用实例
阅读原文时间:2023年07月11日阅读:1

C++ Multimap运用实例

#include
#include
#include
#include

using namespace std;

int main()
{
multimap 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() << "english german " << endl;  
cout << setfill('-') << setw() << "  " << setfill(' ') << endl;

for (const auto& elem1:dict1)  
{  
    cout << "  " << setw() << " " << 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 ;  

}

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版)