美文网首页
C++ Map用法

C++ Map用法

作者: Elijah_cs | 来源:发表于2019-07-06 19:20 被阅读0次

最近在想着用map的结构来代替数组以减少遍历来提高时间效率,故看下map的用法

  1. map简介
    map是STL的一个关联容器,它以<key,value>一对一的形式存储,且map的内部自建一个红黑树,使得其可以自动排序.
  • key可以时任意的数据类型,比如int,char,包括用户自定义数据类型
  • value是该key对应的存储的值


    tree
  1. usage
    使用时要先加入头文件
    #include<map>
    使用格式如下:

map<datatype,datatype> Exm;

datatype即为数据类型

  1. insert

插入数据的方式由三种,

map<int,int> Exm;
#利用insert函数插入pair
Exm.insert(pair<int,int>(0,1));
#利用insert函数插入value_type
Exm.insert(map<int,int>::value_type(2,3));
#用Array方式插入
Exm[4] = 5

利用insert插入的时候,当遇到相同的key时,它不会进行插入操作即覆盖原来的数据,但是利用数组的方式插入时是可以进行
如何判断是否插入成功呢,可以用pair来判断:

pair<map<int,string>::iterator,bool> Pair_Exm;
Pair_Exm = Exm.insert(map<int,int>::value_type(2,3));

如果Pair_Exm == true那么是插入成功了,map的大小可以用size()来获得,即Exm.size()

  1. map的遍历
    有三种方法对其进行遍历,1前向迭代器 2 反向迭代器 3 数组的形式
    迭代器:
map<int,int>::iterator iter;      //迭代器
map<int,int>::reverse_iterator r_iter;  //反向迭代器
for(iter = Exm.begin() ; iter != Exm.end() ; iter++ )
{//正向遍历
    "#ensp;" cout<<iter->first<<"   <<iter->second<<endl;
}
for(r_iter = Exm.rbegin(); r_iter != Exm.rend() ; r_iter++ )
{
      cout<<r_iter->first<<"   "<<r_iter->second<<endl;
}
#map的基本操作函数
begin()   //返回指向头部的迭代器
clear()    //删除所有元素
count()   //返回指定元素出现的次数
end()      //返回指向尾部的迭代器
insert()   //插入元素

5.代码实践
重温map结构是为了减少时间,提高效率,情况是这样的:现在有两个整数i,j在0~255之间,它们一起对应一个数count,如果用一个数组进行存储的话就是Array[i][j] = count,那么在遍历的时候就需要进行256*256次操作,时间代价太大,所以想用map来进行存储,下面进行尝试:

UInt TComRdCost::calcDWT(Pel* pi0, Int stride, Int width, Int height)
{ 
    map<vector<int>, int> RGB_Array;
    map<vector<int>, int>::iterator iter;
    int i,j;
    //定义一个计数器数组,其中i,j分别为当前的RGB值以及其附近RGB均值
    //利用RBB和RGB_Sur的信息作为判断依据
    short int count[256][256] = {};
    int AREA = width*width;
    int Rgb_sur=0;
    int Rgb = 0;
    double entropy = 0;
    double pij = 0;
    double delat; 
    //遍历像素矩阵,统计频率
    for(i=0;i<width;i++)
    {
        for(j=0;j<width;j++)
        {
            //ofsinfo<<pi0[i*stride+j]<<" ";
            //得到i,j位置上的八通道的均值像素值
            Rgb_sur = calSur(i,j,pi0,width,stride);
            Rgb = pi0[i*stride+j];
            //该像素的值,均值像素的计数增加1
            count[Rgb][Rgb_sur]++;
            //map的键为一个vector数组,往其中插入像素值和像素均值
            vector<int> data;
            data.push_back(Rgb);
            data.push_back(Rgb_sur);
            //更新map中的计数
            RGB_Array[data] = count[Rgb][Rgb_sur];
            //ofsinfo<<Rgb<<"  ---   "<<Rgb_sur<<"   ****  "<<endl;
            //RGB_Array.insert(map<int, int>::value_type(Rgb*Rgb_sur,count[Rgb][Rgb_sur]));
        }
        //ofsinfo<<"stop  "<<endl;
        //ofsinfo<<endl;
    }
    //ofsinfo<<"mapsize()  "<<RGB_Array.size()<<endl;
    for(iter = RGB_Array.begin();iter !=RGB_Array.end(); iter++)
    {
        //ofsinfo<<"mapdata, count:  "<<iter->second<<" ****"<<endl;
        //pij = double(count[i][j])/AREA;
        pij = double(iter->second)/AREA;
        delat = pij*log(pij)/log(2);
        entropy -= pij*log(pij)/log(2);
    }
    //ofsinfo<<"entropy"<<entropy<<endl;
    if(entropy<0.7)
    {
        return 0;
    }
    else
    {
        return 1;
    }
    return 1;

}

完成任务。

相关文章

网友评论

      本文标题:C++ Map用法

      本文链接:https://www.haomeiwen.com/subject/xgoshctx.html