积木

No sub title

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  140 Posts :: 1 Stories :: 11 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

C++ Map 容器操作实例

// Test_20110514_1853.cpp : Defines the entry point for the console application.
//

//说明:最下面有总结说明。

#include 
"stdafx.h"

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

typedef map
<intstring> TMyMap;

void PrintSplitterString(short sCount = 50)
{
    
if (sCount < 20 || sCount > 80)
        sCount 
= 50;
    
for (short i = 1; i <= sCount; i++)
        cout 
<< "-";
    cout 
<< endl;
}

void CoutMapList(const TMyMap& mmMap)
{
    
for (TMyMap::const_iterator iter = mmMap.begin(); iter != mmMap.end(); iter++)
    {
        
string str = iter->second;
        cout 
<< "键:" << iter->first << "      值:" << str.c_str() << endl;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
//*****************************************************************************************
// * 在此介绍map容器的用法
//*****************************************************************************************

    
//1.创建一个map容器变量
    TMyMap enumMap;

    
//往map中加入元素
    PrintSplitterString();
    cout 
<< "操作:往map 中添加元素方法一" << endl;
    enumMap[
1= "One";
    enumMap[
2= "Two";
    enumMap[
2= "Seven";    //此时将是修改键为的值,如果该键元素不存在,则新建一个
    CoutMapList(enumMap);

    
//////////////////////////////////////////////////////////////////////////
    //* 说明:使用此方法的优点是:十分的方便。
    
//* 缺点:效果不高。因为map 类已经对[] 操作符进行了重载
    
//*          比如,在插入第个元素时,系统需要先在enumMap中查找主键为的项,没有发现,然后新的对象插入enumMap,键是
    
//*       1,值是一个空的字符串。插入完成后,再将将显式的Two字符串,赋给值。如果我们要插入的是一个类对象了?
    
//*       那将会是个很大的开销。
    
//*       缺点:如果先前已经存在了键为的元素时,则将变成值修改
    //////////////////////////////////////////////////////////////////////////

    cout 
<< endl << "操作:往map 中添加元素方法二" << endl;
    enumMap.insert(TMyMap::value_type(
2"Third"));
    
//////////////////////////////////////////////////////////////////////////
    //* 说明:此方法解决了上面方法的缺点,但有个地方需要注意:
    
//* 如果列表中已经存在键为的元素时,它是不会插入新的元素的。
    //////////////////////////////////////////////////////////////////////////
    enumMap.insert(TMyMap::value_type(3"Third"));
    CoutMapList(enumMap);
    PrintSplitterString();

    
//查找并获取map中的元素
    PrintSplitterString();
    cout 
<< "操作:查找并获取map 中元素" << endl;
    
//方法一:通过下标获得一个值(此适用在下标明确的情况下)
    string tmp = enumMap[4];    //此方法总是安全的。就算不存在键为的元素,系统也不会报,只是返回的值是空的。因为系统是会新创建一个键为的节点,(值当然为空)
    cout << tmp.c_str() << endl;
    
    
//另一种取得元素的方法是,通过查找
    TMyMap::const_iterator iter_find = enumMap.find(2);
    
if (iter_find != enumMap.end())
    {
        
//找到了
        cout << iter_find->second.c_str() << endl;
    }
    
else
    {
        
//没有找到
        cout << "没有找到。" << endl;
    }
    PrintSplitterString();

    
//从map 中删除元素
    PrintSplitterString();
    cout 
<< "操作:从map 中删除元素的方法" << endl;
    
//////////////////////////////////////////////////////////////////////////
    //* 删除的方法有:
    
//* iterator erase(iterator it);//通过一个条目对象删除
    
//* iterator erase(iterator first, iterator last);//删除一个范围
    
//* size_type erase(const key& key);//通过键来删除
    
//* clear();//相漕运于enumMap.erase(enumMap.begin(), enumMap.end());
    //////////////////////////////////////////////////////////////////////////
    cout << "==> 全部列表如下:" << endl;
    CoutMapList(enumMap);
    enumMap.erase(
3);
    cout 
<< "==> 删除掉键值为:的" << endl;
    CoutMapList(enumMap);
    cout 
<< endl << endl;
    enumMap.erase(enumMap.begin());
    cout 
<< "==> 删除掉第一个元素" << endl;
    CoutMapList(enumMap);
    cout 
<< endl << endl;
    enumMap.clear();
    cout 
<< "==> 全部清空" << endl;
    CoutMapList(enumMap);
    PrintSplitterString();

    
//所有操作均已经结束
    PrintSplitterString();
    cout 
<< "所有操作均已经结束" << endl;
    PrintSplitterString();
    cout 
<< endl;


//*********************************************************************************************
//* 现在总结如下:
//* 对于map的操作。最好不要通过下标进行。比如:想要取一个元素,要先通过查找,有了,再操作,没有。不操作。
//* 否则有可能系统会为你自动添加 “莫名奇妙” 的元素进去你都不知道
//*********************************************************************************************

    
return 0;
}

posted on 2011-07-04 20:30 Jacc.Kim 阅读(2431) 评论(0)  编辑 收藏 引用 所属分类: VC / C++

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