huyutian

他强由他强,清风拂山岗;他横由他横,明月照大江。他自狠来他自恶,我自一口真气足

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  20 随笔 :: 47 文章 :: 22 评论 :: 0 Trackbacks
/*
自动建立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

posted on 2010-02-07 23:57 胡雨田 阅读(633) 评论(0)  编辑 收藏 引用 所属分类: 编程技巧

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理