zoukankan      html  css  js  c++  java
  • multimap用法

    multimap的特点为key是可以重复的,而普通map中的key是不可以重复的。
    声明

    multimap<int, CString>mapTest;
    multimap<int, CString>::iterator pIter;
    typedef multimap<int, CString>::iterator it;
    
    插入,跟普通map相似
            mapTest.insert(PairTest(1, _T("a")));
            mapTest.insert(PairTest(1, _T("b")));
    	mapTest.insert(PairTest(1, _T("c")));
    	mapTest.insert(PairTest(2, _T("a")));
    

    遍历,主要思路为根据key,multimap的特点为key是可以重复,即一个key对应多个value。将所有key取出来,然后每个key逐个遍历。

      1取出所有的key,可以用set

            for (pIter = mapTest.begin(); pIter != mapTest.end(); pIter++)
    	{
    		setKey.insert(pIter->first);
    	}
    
    2.逐个key遍历
            pair<it, it> PairIter;
    	for (pIterKeySet = setKey.begin(); pIterKeySet != setKey.end(); pIterKeySet++)
    	{
    		int iCurrentKey = *pIterKeySet;
    		PairIter = mapTest.equal_range(iCurrentKey);
    		while (PairIter.first != PairIter.second)
    		{
    			CString csTempKey = PairIter.first->second;
    			cout<<csTempKey<<endl;
    			PairIter.first++;       //这个要的
    		}
    	}
    
    这样就可以逐个遍历了
  • 相关阅读:
    线程学习笔记(一)
    进程间通信
    管道通信操作
    在程序中执行shell命令
    进程控制(一)
    Makefile文件学习总结
    进程学习笔记
    C#不安全代码和指针
    Unity3D ShaderLab 修改渲染队列进行深度排序
    Unity3D ShaderLab 透明裁剪着色器
  • 原文地址:https://www.cnblogs.com/flysnail/p/2095475.html
Copyright © 2011-2022 走看看