[c++11]std::map

学习网址:

std::map::emplace
map::emplace - C++ Reference

C++ map用法总结(整理)
C++ map用法总结(整理)_sevenjoin的博客-CSDN博客_c++map

unordered_map和map的区别

1.看底层实现:
  map          的底层实现是      红黑树
  unordered_map的底层实现是      哈希表

  所以map内部的元素是有序的,而unordered_map的底层是无序的。

2.看开销
  map          : 占用空间大 
  unordered_map:  占用空间小

  由于map的底层使用的是红黑树,每个节点都需要额外的保存父节点,孩子节点和红/黑性质导致。

3. 查找速度
   map          : 慢
   unordered_map: 非常快

   对于unordered_map,底层实现是哈希表,所以其查找速度会非常快。
   那有什么不好的地方?哈希表的建立会比较麻烦一些,因为要解决哈希冲突。

4.遍历顺序:
    对于unordered_map或unordered_set容器,其遍历顺序与创建该容器时输入的顺序不一定相同,因为遍历是按照哈希表从前往后依次遍历的。

总结:
    除非是对顺序有特殊要求的场景,不然一般不去选择map。

不要在for循环里erase

	for (auto &x : _CameraParamMap)
	{
		_CameraParamMap.erase(x.first);//不要这么用,会崩掉的
	}

参考下面的写法:
for(vector<int>::iterator iter=veci.begin(); iter!=veci.end(); )
{
     if( *iter == 3)
      { 
           //返回删除后的下一个,不然iter会变成野指针
           iter = veci.erase(iter);
      }
      else
            iter ++ ;
}

C ++ map emplace()函数的解释

用于通过在容器中插入新元素来扩展map容器。元素是直接构建的(既不复制也不移动)。

通过给传递给该函数的参数args调用元素的构造函数。仅当键不存在时才进行插入。


 dma_map[5]=true; //[]中括号这种方式也表示插入

下面的例子用到emplace和function,来响应不同字符对应的函数

/*
 *实验map
 *
 */

#include <signal.h>
#include <string>
#include <sstream>
#include <iostream>
#include <memory>

#include <map>


using namespace std;


using XmlApi = std::function<void(std::string &strXml )>;

static map<string, XmlApi> s_map_api;

static map<string, int> s_map_int;


void api_regist(const string &api_path, const XmlApi &func) {
    s_map_api.emplace(api_path, func);
}

void api_regist(const string &api_path, int x) {
    s_map_int.emplace(api_path, x);
}


void p1(std::string &strXml){
    std::cout<<"p1="<<strXml<<endl;

}


void p2(std::string &strXml){
    std::cout<<"p2="<<strXml<<endl;

}


//测试 函数指针
void test_map()
{
    api_regist("p1", p1);
    api_regist("p2", p2);


    std::string url="p2";

    std::string strXml="hello";

    auto it = s_map_api.find(url);
    if (it == s_map_api.end()) {
        return;
    }

    it->second(strXml);

    std::cout << "map contains:"<<endl;
    for (auto& x: s_map_api)
    {
        std::cout << x.first << ":";
        x.second(strXml);
    }
    std::cout << '\n';

}

//测试 int
void test_mapInt()
{
    api_regist("p1", 1);
    api_regist("p2", 2);


    std::string url="p2";

    auto it = s_map_int.find(url);
    if (it == s_map_int.end()) {
        return;
    }

    std::cout<<"num=" << it->second << endl;

    std::cout << "map contains:"<<endl;
    for (auto& x: s_map_int)
    {
        std::cout << x.first << ":"<<x.second<<endl;
    }

    std::cout << '\n';

}

//测试 buffer的使用情况
void test_map_queue()
{
    map<int, bool> dma_map;

    //init
    for(int i=0;i<3;i++){
        dma_map.emplace(i, false);
    }

   //print
    for (auto& dma: dma_map)
    {
        std::cout << dma.first << ":"<<dma.second<<endl;
    }

    //find
    int fd=-1;
    for (auto& dma: dma_map)
    {
        if(!dma.second){
            fd = dma.first;
            dma.second = true;
            break;
        }
    }

    //find nothing
    if(fd == -1){
        std::cout <<"fd="<<fd<<endl;
    }
    std::cout <<"fd="<<fd<<endl;



    auto it = dma_map.find(fd);
    if (it == dma_map.end()) {
        std::cout <<"error!!"<<endl;
        return;
    }


    std::cout <<"result:" <<it->first << "="<<it->second<<endl;
      it->second = false;
    std::cout <<"modify:" <<it->first << "="<<it->second<<endl;

}


int main()
{

    //test_map();
    //test_mapInt();
    test_map_queue();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值