map的用法

依然是一点简简单单的代码,拷贝下去运行一下,应该就知道map怎么用了,不想解释map到底是什么,有什么伟大的意义……
//------------------------------------------------------------------------------
// Copyright (C), 2008-2009, Zhengzz Co.,Ltd,All Rights Reserved
//
// FileName:    Vector.cpp
//
// Author:      zhengzz
//
// Date:        2009-06-10
//
// Description: map的用法
//
// History:     <author>zhengzongzhao<time>2009-06-10<version>1.0
//
//------------------------------------------------------------------------------
#include <string>
#include <map>
using namespace std;
typedef std::map<int , char *> DataMap;
typedef pair<int, char*> prNode;
void InitMap(DataMap *dmWord);
int main()
{
    DataMap           dmWord;
    int               nIndex;
    DataMap::iterator iter;
    nIndex = 0;
    InitMap(&dmWord);
   
    dmWord[6] = "ddd";
    dmWord[7] = "eee";
    printf("初始化之后MAP中的数据为:/n");
    for ( iter = dmWord.begin(); iter != dmWord.end(); iter++ )
    {
        printf("%d--%s/n", iter->first, iter->second);
    }
    printf("/n请输入需要查询数据序号:");
    scanf("%d", &nIndex);
    printf("你所查询的数据是%s/n", dmWord[nIndex]);
    printf("查询之后MAP中的数据为:/n");
    for ( iter = dmWord.begin(); iter != dmWord.end(); iter++ )
    {
        printf("%d--%s/n", iter->first, iter->second);
    }
    printf("/n请输入需要删除数据序号:");
    scanf("%d", &nIndex);
    printf("你需要所删除的数据是%s/n", dmWord[nIndex]);
    dmWord.erase(nIndex);
    printf("删除之后MAP中的数据为:/n");
    for ( iter = dmWord.begin(); iter != dmWord.end(); iter++ )
    {
        printf("%d--%s/n", iter->first, iter->second);
    }
    getchar();
    getchar();
 return 0;
}
void InitMap(DataMap *dmWord)
{
    dmWord->insert(prNode(0, "praaa"));
    dmWord->insert(prNode(1, "prbbb"));
    dmWord->insert(prNode(2, "prccc"));
    dmWord->insert(DataMap::value_type(3, "vtaaa"));
    dmWord->insert(DataMap::value_type(4, "vtbbb"));
    dmWord->insert(DataMap::value_type(5, "vtccc"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值