C++ Map映射

本文章所有内容源于https://blog.csdn.net/qq_38609565/article/details/107938982


1. 定义

键名:就是存的值的编号
键值:就是要存放的数据

映射是一种关联容器,存储键值和映射值按特定顺序组合而成的元素。
在这里插入图片描述

2. 功能:

 begin()         返回指向map头部的迭代器
 clear()        删除所有元素
 count()         返回指定元素出现的次数
 empty()         如果map为空则返回true
 end()           返回指向map末尾的迭代器
 erase()         删除一个元素
 find()          查找一个元素
 insert()        插入元素
 key_comp()      返回比较元素key的函数
 max_size()      返回可以容纳的最大元素个数
 rbegin()        返回一个指向map尾部的逆向迭代器
 rend()          返回一个指向map头部的逆向迭代器
 size()          返回map中元素的个数
 swap()           交换两个map
 lower_bound()   返回键值>=给定元素的第一个位置
 upper_bound()    返回键值>给定元素的第一个位置
 value_comp()     返回比较元素value的函数

3. 用法:

#include <iostream>
#include <string>
#include <map>
 
using namespace std;
 
int main() {
 
    map<int, string> students;//声明 
 
    //第一种插入方式 
    pair<int, string> value(1, "aaa");
    students.insert(value);
 
    //第二种插入方式  
    students.insert(map<int, string>::value_type(2, "bbb"));
    students.insert(map<int, string>::value_type(3, "ccc"));
 
    /************************************查找**********************************/
    //迭代器遍历 
    map<int, string>::iterator iter1; 
    cout << "迭代器遍历" << endl;
    for (iter1 = students.begin(); iter1 != students.end(); iter1++) {
        //输出first(键)和second(值) 
        cout <<iter1->first << ' ' << iter1->second << endl;
    }
      
    //反向迭代器遍历 
    map<int, string>::reverse_iterator iter2;
    cout << "反向迭代器遍历 " << endl;
    for (iter2 = students.rbegin(); iter2 != students.rend(); iter2++) {
        //输出first和second 
        cout <<iter2->first << ' ' << iter2->second << endl;
    }
 
    //数组遍历 
    cout << "数组遍历" << endl;
    size_t n = students.size();//map的大小
    for (int i = 1; i <= n; i++) {
        cout << i << " " << students[i] << endl;
    }
 
    //通过find查找
    iter1 = students.find(2);
    if (iter1 != students.end()) {
        cout << "Find, the value is " << iter1->second << endl;
    }
    else {
        cout << "Do not Find" << endl;
    }
    /**************************************************************************/
 
    /************************************修改**********************************/
    iter1 = students.find(1);
    //修改值,键名不可修改
    iter1->second = "fff";
    cout << "修改" << endl;
    for (iter2 = students.rbegin(); iter2 != students.rend(); iter2++) {
        //输出first和second 
        cout << iter2->first << ' ' << iter2->second << endl;
    }
    /**************************************************************************/
 
    /************************************插入**********************************/
    students.insert(map<int, string>::value_type(4, "ddd"));
    cout << "插入" << endl;
    for (iter2 = students.rbegin(); iter2 != students.rend(); iter2++) {
        //输出first和second 
        cout << iter2->first << ' ' << iter2->second << endl;
    }
    /**************************************************************************/
 
    /************************************删除************************************/
    students.erase(4);
    cout << "删除" << endl;
    //先查找后删除
    //iter1 = students.find(1);
    //students.erase(iter1);
    //如果删除了会返回1,否则返回0  
    //size_t x = students.erase(1); 
 
    for (iter2 = students.rbegin(); iter2 != students.rend(); iter2++) {
        //输出first和second 
        cout << iter2->first << ' ' << iter2->second << endl;
    }
 
    students.clear();
    //students.erase(students.begin(), students.end()); //用迭代器,成片的删除相当于clear  
    if(students.empty()){
        cout << "清空" << endl;
    }
    /**************************************************************************/
 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值