STL之Map使用小结

/*
自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。 
快速插入Key - Value 记录。 
快速删除记录 
根据Key 修改value记录。 
遍历所有记录。
MAP的插入方式跟别的容器有所不同:除数组形式插入方式外,插入元素时,如果key值相同,则不做插入动作,
//建议:特殊元素的插入前先遍历是否存在,若存在则将其删除后再插入

*/


#include 
<iostream>
#include 
<map>
#include 
<string>
using namespace  std;

void printMap( map<const char*double> &
m)
{
map
<const char*double>
::iterator iter;
for (iter = m.begin(); iter !=m.end(); iter++
)
{
    cout
<<iter->first<<""<<iter->second<<
endl;
}

}


void  main()
{
bool bRet = false
;
string
 str;
//
创建map对象
//创建一个没有任何元素的map对象m,元素的键值类型是char,映照数据类型为int,键值的比较函数对象为greater<char>

map<charint, greater<char> > m1; 
//
创建一个map对象m2,元素的键值类型为const char*, 映照数据类型为int, 键值的比较函数对象为strLess
//map<const char *, int> m(strLess()); 

map<const char*double> m2;
map
<const char*double> m3(m2); //用一个map容器的元素和比较函数拷贝生成一个新的map容器对象


pair
<const char*double> p1("a",3.6);
pair
<const char*double> p2("b"3.2
);
pair
<const char*double> pairArray[] = {p1,p2}
;
map
<const char*double> m4(pairArray, pairArray+2); //拷贝迭代区间[first, last)所指的数据,创建一个map对象


cout
<<"printMap(m4): "<<endl; 
printMap(m4);

//元素插入 1-value-type (该方法又细分为4种: value_type, pair, make_pair, 下标方式)

cout<<endl<<"m4.insert( map<const char*, double>::value_type(\"c\", 1.8) ) = "<<endl;
bRet 
= m4.insert( map<const char*double>::value_type("c"1.8
) ) .second; 
if
 (bRet) 
    cout
<<"OOOOK!"<<
endl;
else

    cout
<<"FFFFailed!"<<endl;
printMap(m4);


cout
<<"m4.insert( pair<const char*, double>(\"d\", 2.3) ) = "<<
endl;
str 
= (m4.insert( pair<const char*double>("d"2.3) )).second ? string("OK") : string("Failed!!"
);
cout
<<str<<
endl; 
printMap(m4);

cout
<<"m4.insert( make_pair(\"e\", 6.3) ) = "<<
endl;
//m4.insert( map<const char*, double>::value_type("e", 6.3) );

str = (m4.insert( make_pair("e"6.3) ) ).second ? string("OK") : string("Failed!!");
cout
<<str<<
endl; 
printMap(m4);

cout
<<"m4.insert(make_pair(\"e\", 10.2) ) : \nin this way, reinsert the same elem will failed "<<
endl;
str
= ( (m4.insert(make_pair("e"10.2))).second ) ? string("OK") : string("Failed!!"
);
cout
<<str<<
endl; 
printMap(m4);

cout
<<"m4[\"e\"] = 10.2 will OK, \nbut maybe low-level efficiency, and dont return ture/false."<<
endl;
m4[
"e"= 10.2
;
printMap(m4);
//
元素插入:2-在某位置前插入 insert(&pos, elem)
//
元素插入:3-迭代区间的插入 insert(&first, &last)
//


//
元素删除 键值删除用size_type erase(elem), 迭代器位置上的元素删除用void erase(&pos); 
//迭代区间[&first, &last)元素删除用void erase(&first, &last) , 删除所有元素用void clear()


cout
<<endl<<"m4.erase(\"100\") = "<<endl;
    m4.erase(
"100"
);
printMap(m4);

cout
<<"m4.erase(\"c\") = "<<
endl;
m4.erase(
"c"
);
printMap(m4);

//搜索元素

cout<<endl<<"m4.find(\"b\") : "<<endl;
map
<const char*double>
::iterator iterFind1;
iterFind1 
= m4.find("b"
);
if (iterFind1 !=
 m4.end())
    cout
<<(*iterFind1).second<<
endl;
else

   cout
<<"not found!"<<endl;

cout
<<"m4.find(\"c\") : "<<
endl;
map
<const char*double>
::iterator iterFind2;
iterFind2 
= m4.find("c"
);
if (iterFind2 !=
 m4.end())
    cout
<<(*iterFind2).second<<
endl;
else

   cout
<<"not found!"<<endl;

cout
<<endl<<"Other: "<<
endl;
cout
<<"m4.empty() = "<<m4.empty()<<
endl;
cout
<<"m4.size() = "<<m4.size()<<", m4.max_size() = (hex)"<<hex<<m4.max_size()<<
endl;
}



//=================测试结=========================
printMap(m4):
b: 
3.2

a: 
3.6

m4.insert( map
<const char*double>::value_type("c
OOOOK!
c: 
1.8
b: 
3.2
a: 
3.6
m4.insert( pair
<const char*double>("d"2.3) ) =
OK
d: 
2.3
c: 
1.8
b: 
3.2
a: 
3.6
m4.insert( make_pair(
"e"6.3) ) =
OK
e: 
6.3
d: 
2.3
c: 
1.8
b: 
3.2
a: 
3.6
m4.insert(make_pair(
"e"10.2 ) ) :
in this
 way, reinsert the same elem will failed
Failed
!!

e: 
6.3
d: 
2.3
c: 
1.8
b: 
3.2
a: 
3.6
m4[
"e"= 10.2  will OK,
but maybe low
-level efficiency, and dont return
 tu
e: 
10.2

d: 
2.3
c: 
1.8
b: 
3.2
a: 
3.6

m4.erase(
"100"=
e: 
10.2
d: 
2.3
c: 
1.8
b: 
3.2
a: 
3.6
m4.erase(
"c"=
e: 
10.2
d: 
2.3
b: 
3.2
a: 
3.6

m4.find(
"b" ) :
3.2

m4.find(
"c" ) :
not found
!


Other:
m4.empty() 
= 0
m4.size() 
= 4, m4.max_size() =  (hex)fffffff
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值