最近在想著用map的結(jié)構(gòu)來代替數(shù)組以減少遍歷來提高時(shí)間效率宙拉,故看下map的用法
- map簡(jiǎn)介
map是STL的一個(gè)關(guān)聯(lián)容器湿镀,它以<key,value>一對(duì)一的形式存儲(chǔ),且map的內(nèi)部自建一個(gè)紅黑樹恢总,使得其可以自動(dòng)排序.
- key可以時(shí)任意的數(shù)據(jù)類型彪笼,比如int,char,包括用戶自定義數(shù)據(jù)類型
-
value是該key對(duì)應(yīng)的存儲(chǔ)的值
tree
- usage
使用時(shí)要先加入頭文件
#include<map>
使用格式如下:
map<datatype,datatype> Exm;
datatype即為數(shù)據(jù)類型
- insert
插入數(shù)據(jù)的方式由三種淆九,
map<int,int> Exm;
#利用insert函數(shù)插入pair
Exm.insert(pair<int,int>(0,1));
#利用insert函數(shù)插入value_type
Exm.insert(map<int,int>::value_type(2,3));
#用Array方式插入
Exm[4] = 5
利用insert插入的時(shí)候统锤,當(dāng)遇到相同的key時(shí)毛俏,它不會(huì)進(jìn)行插入操作即覆蓋原來的數(shù)據(jù),但是利用數(shù)組的方式插入時(shí)是可以進(jìn)行
如何判斷是否插入成功呢饲窿,可以用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()
- map的遍歷
有三種方法對(duì)其進(jìn)行遍歷逾雄,1前向迭代器 2 反向迭代器 3 數(shù)組的形式
迭代器:
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的基本操作函數(shù)
begin() //返回指向頭部的迭代器
clear() //刪除所有元素
count() //返回指定元素出現(xiàn)的次數(shù)
end() //返回指向尾部的迭代器
insert() //插入元素
5.代碼實(shí)踐
重溫map結(jié)構(gòu)是為了減少時(shí)間阀溶,提高效率,情況是這樣的:現(xiàn)在有兩個(gè)整數(shù)i,j在0~255之間鸦泳,它們一起對(duì)應(yīng)一個(gè)數(shù)count,如果用一個(gè)數(shù)組進(jìn)行存儲(chǔ)的話就是Array[i][j] = count银锻,那么在遍歷的時(shí)候就需要進(jìn)行256*256次操作,時(shí)間代價(jià)太大做鹰,所以想用map來進(jìn)行存儲(chǔ)击纬,下面進(jìn)行嘗試:
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;
//定義一個(gè)計(jì)數(shù)器數(shù)組,其中i,j分別為當(dāng)前的RGB值以及其附近RGB均值
//利用RBB和RGB_Sur的信息作為判斷依據(jù)
short int count[256][256] = {};
int AREA = width*width;
int Rgb_sur=0;
int Rgb = 0;
double entropy = 0;
double pij = 0;
double delat;
//遍歷像素矩陣钾麸,統(tǒng)計(jì)頻率
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];
//該像素的值更振,均值像素的計(jì)數(shù)增加1
count[Rgb][Rgb_sur]++;
//map的鍵為一個(gè)vector數(shù)組,往其中插入像素值和像素均值
vector<int> data;
data.push_back(Rgb);
data.push_back(Rgb_sur);
//更新map中的計(jì)數(shù)
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;
}
完成任務(wù)饭尝。