c++中STL中map的基本用法

c++中STL中map的基本用法

具体的详见代码或者 http://www.cplusplus.com/reference/map/map/


// map的基本使用
#include "pch.h"
#include <iostream>
//map需要包含的头文件
#include <map>
using namespace std;
int main()
{
    //定义一个map
    std::map<int,std::string> map_test;
    //map的插入方式
    //1、insert方法  pair 形式
    map_test.insert(std::pair<int, std:: string>(1, "one"));  
    map_test.insert(std::pair<int, std::string>(2, "two"));  
    map_test.insert(std::pair<int, std::string>(3, "three"));
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    //2、insert方法  value_type 形式
    map_test.insert(map<int, string>::value_type (4, "four"));  
    map_test.insert(map<int, string>::value_type (5, "five"));  
    map_test.insert(map<int, string>::value_type (6, "six")); 
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    //3、数组的形式
    map_test[7] = "seven";  
    map_test[8] = "eight";  
    map_test[9] = "nine";
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");

    // 查看insert方法是否成功
    pair<map<int, string>::iterator, bool> Insert_Pair;  
    Insert_Pair = map_test.insert(pair<int, string>(1, "one"));  
    if(Insert_Pair.second == true)
    {
        cout<<"Insert Successfully"<<endl;
    }     
    else
    {
        cout<<"Insert Failure"<<endl;
    }
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");

    Insert_Pair = map_test.insert(pair<int, string>(1, "one_repeat"));  
    if(Insert_Pair.second == true)
    {
        cout<<"Insert Successfully"<<endl;
    }     
    else
    {
        cout<<"Insert Failure"<<endl;
    }
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    //通过数组可以成功其value值
    map_test[1] = "one_array";
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    //通过迭代器改变其value值
    auto iter1 = map_test.begin();
    iter1->second = "one_iter";
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");

    // 利用map的内置函数查看是否有该元素
    auto iter2 = map_test.find(1);
    if(iter2 != map_test.end())
    {
        printf("find %d--%s\n",iter2->first,(iter2->second).c_str());
    }
    else
    {
        printf("not find\n");
    }
    
    //利用数组下标直接访问存在的元素
    printf(" %s %d  %d \n",map_test[1].c_str(),map_test[1].size(),map_test[1].length());
    //利用数组下标直接访问不存在的元素
    printf(" %s %d  %d \n",map_test[11].c_str(),map_test[11].size(),map_test[11].length());


    //删除一个元素
    auto iter3 = map_test.begin();
    map_test.erase(iter3);
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    //删除存在的元素  返回值为 1
    int n = map_test.erase(2);
    if(n == 1)
    {
        printf("n %d \n",n);
    }
    else
    {
       printf("n %d \n",n);
    }
    //删除不存在的元素  返回值为 1
    n = map_test.erase(22);
    if(n == 1)
    {
        printf("n %d \n",n);
    }
    else
    {
       printf("n %d \n",n);
    }
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");
    // 清空元素 
    #if 0
    map_test.erase(map_test.begin(),map_test.end());
    #else
    map_test.clear();
    #endif
    printf("*************************************\n");
    for(auto iter = map_test.begin(); iter != map_test.end(); iter++)
    {
        printf("%d %s\n",iter->first,(iter->second).c_str());
    }
    printf("*************************************\n");

    
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值