C++ STL multimap容器

一、前言

multimap容器和map容器基本类似，在这里只说明map容器没有的内容，或者与map容器的区别。

二、方法

1、获取值

1）方法1

``````std::multimap<int, string> multimap{ {1,"lin"},{1,"wen"},{1, "kai"},{2,"li"},{2,"wu"},{3,"huang"} };

int nCount = multimap.count(1);
std::cout << "nCount: " << nCount << std::endl;

std::multimap<int, string>::iterator mtmapIter = multimap.begin();
for (int i = 0; i < nCount; i++)
{
std::cout << mtmapIter->first << "  " << mtmapIter->second << std::endl;
++mtmapIter;
}``````

2）方法2

``````std::multimap<int, string> multimap{ {1,"lin"},{1,"wen"},{1, "kai"},{2,"li"},{2,"wu"},{3,"huang"} };

int nCount = multimap.count(1);
std::cout << "nCount: " << nCount << std::endl;
std::multimap<int, string>::iterator mtmapBeginIter = multimap.lower_bound(1);

for (; mtmapBeginIter != multimap.upper_bound(1); mtmapBeginIter++)
{
std::cout << mtmapBeginIter->first << "  " << mtmapBeginIter->second << std::endl;
}``````

3）方法3

``````std::multimap<int, string> multimap{ {1,"lin"},{1,"wen"},{1, "kai"},{2,"li"},{2,"wu"},{3,"huang"} };

int nCount = multimap.count(1);
std::cout << "nCount: " << nCount << std::endl;
pair<std::multimap<int, string>::iterator, std::multimap<int, string>::iterator> iter = multimap.equal_range(1);

for (; iter.first != iter.second; iter.first++)
{
std::cout << iter.first->first << "  " << iter.first->second << std::endl;
}``````

1 声望
0 粉丝
0 条评论