map应用

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
    //1.用insert函数插入pair数据;
    
    map<int,string> mapstudent;
    mapstudent.insert(pair<int, string>(1,"stu1"));
    mapstudent.insert(pair<int, string>(2,"stu2"));
    mapstudent.insert(pair<int, string>(3,"stu3"));
    
    /*前向迭代器
    map<int, string>::iterator iter;
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    */
    
    /*反向迭代器
    map<int, string>::reverse_iterator iter;
    for(iter = mapstudent.rbegin(); iter != mapstudent.rend(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    */
    
    /*数组的方式遍历map容器
    int nSize = mapstudent.size();
    for (int index = 1; index <= nSize; index++) {
        cout << mapstudent[index] << endl;
    }
    */
    
    //2.用insert 函数插入value_type数据
    /*
    map<int,string> mapstudent;
    mapstudent.insert(map<int, string>::value_type (1, "stu1"));
    mapstudent.insert(map<int, string>::value_type (2, "stu2"));
    mapstudent.insert(map<int, string>::value_type (3, "stu3"));
    
    map<int, string>::iterator iter;
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    */
    
    /*
    //3.用数组方式插入数据
    map<int ,string> mapstudent;
    mapstudent[1] = "stu1";
    mapstudent[2] = "stu2";
    mapstudent[3] = "stu3";
    map<int, string>::iterator iter;
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    //前2种插入数据方法中,当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值
    */
    
    //4.前2种插入数据方法怎么知道是否插入数据成功
    /*
    map<int, string> mapstudent;
    pair<map<int, string>::iterator, bool>  insert_pair;
    insert_pair = mapstudent.insert(pair<int, string>(1, "stu1"));
    if(insert_pair.second == true) {
        cout << "successful" << endl;
    } else {
        cout << "fail" << endl;
    }
    */
    
    //5.map插入数据的大小
    /*int nsize = mapstudent.size();
    cout << "nsize = " << nsize  << endl;
    */
    //6.数据遍历的3种方法  前向迭代器    反向迭代器    数组方法

    //7.数据查找方法:
    //1)用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,
    //由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,
    //要么是0,要么是1,出现的情况,当然是返回1了
    //2)用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,
    //它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭
    //代器等于end函数返回的迭代器
    /*
    map<int, string>::iterator iter;
    iter = mapstudent.find(4);
    if(iter != mapstudent.end()) {
        cout << "find = " << iter->second << endl; 
    } else {
        cout << "Do find = " << endl;
    }
     */
    //8.数据的判空与清空,清空map中数据可以用clear()函数,判定map中是否有数据可以
    //用empty()函数,返回true说明是空map
    
    //9.数据的删除,这里用到erase函数
    //1)要删除1,用迭代器删除
    /*
    map<int ,string>::iterator iter;
    iter = mapstudent.find(1);
    mapstudent.erase(iter);
    
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    */
    //2)删除1,用关键字删除
    /*
    int n = mapstudent.erase(1);
    map<int, string>::iterator iter;
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
    */
    //3)把整个map清空
    mapstudent.erase(mapstudent.begin(), mapstudent.end());
    map<int, string>::iterator iter;
    for(iter = mapstudent.begin(); iter != mapstudent.end(); iter++) {
        cout << iter->first << " " << iter->second << endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屁小猪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值