本文主要是介绍C++ STL multimap用法总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C++ STL multimap用法总结
//*/Author: NYIST_zsj
Date:2014/12/
promble: multimap
主要操作:1、遍历整个multimap2、遍历某个特定的键3、查找某个特定的键4、删除某个特定的键和特定的值///*/#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
/multimap<int,int,greater<int> > mump; //键值从大到小 less<Type> 键值从小到大
typedef multimap<int,int>::iterator Iterator;
int main()
{mump.insert(make_pair(1,2));mump.insert(make_pair(1,4));mump.insert(make_pair(1,5));mump.insert(make_pair(1,6));mump.insert(make_pair(3,4));mump.insert(make_pair(3,5));mump.insert(make_pair(3,4));//遍历整个multimapcout << "---------------visit all multimap------------ " << endl;Iterator it;it = mump.begin();for(it = mump.begin();it != mump.end();++it) {cout << it->first << " " << it->second << endl;}cout << "----------------- End --------------------------- " << endl;//单独遍历只有涉及某个key, equal_range(key)cout << endl << "-----------------visit only one key multimap--------------- " << endl;pair<Iterator,Iterator> it2;it2 = mump.equal_range(1);/*equal_range()返回的是一个pair,该pair的存放的是两个迭代器equal_range().first 存放的迭代器指向的是起始位置equal_range().second 存放的迭代器指向的是末尾的位置有点类似于map中的begin()/end()*/for(it = it2.first;it != it2.second;++it) {cout << it->first << " " << it->second << endl;}cout << " ------------------- End ----------------- " << endl;//删除某个键值,某个值cout << " -----------Delete only one key and only one value ------------- " << endl;it2 = mump.equal_range(1);for(it = it2.first;it != it2.second; ++it) {if(it->first == 1 && it->second == 5) {cout << "delete: " << it->first << " " << it->second << endl;mump.erase(it);}}it = mump.begin();for(;it != mump.end();++it) {cout << it->first << " " << it->second << endl;}cout << "-------------------- End --------------------------- " << endl;//删除某个键值cout << endl << "-------------Delete only one key------------ " << endl;it = mump.find(1);cout << " delete 3 after " << endl;mump.erase(3);it = mump.begin();for(;it != mump.end();++it) {cout << it->first << " " << it->second << endl;}cout << "-------------End----------------" << endl;return 0;
}
这篇关于C++ STL multimap用法总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!