【C++】STL 容器 - map 关联容器 ③ ( map 容器常用 api 操作 | map 容器迭代器遍历 | map#insert 函数返回值处理 )





一、map 容器迭代器遍历



1、map 容器迭代器


C++ 语言中 标准模板库 ( STL ) 的 std::map 容器 提供了 begin() 成员函数 和 end() 成员函数 , 这两个函数 都返回一个迭代器 , 指向容器中的元素 ;

  • std::map#begin() 成员函数 : 该函数返回指向容器中第一个元素的迭代器 ; 对于std::map 容器来说 , 该元素是按键排序后的第一个键值对 ; 如果 map 容器为空 , 则返回的迭代器就是 末尾迭代器 ;
  • std::map#end() 成员函数 : 该函数返回指向容器末尾位置的迭代器 , 末尾位置实际上并不包含任何元素 , 而是作为遍历结束的标志 ; 如果通过 end() 末尾迭代器来访问元素直接崩溃退出 ;

迭代器指向的 map 容器元素说明 : std::map 容器是一个关联容器 , 它存储的元素按键值自动排序 ; 每个元素是一个键值对 对组对象 , 即 std::pair<const Key, T> 类型对象 , 其中 Key 是键类型,T 是值类型 ;


2、代码示例


代码示例 :

#include "iostream"
using namespace std;
#include "map"
#include "string"


int main() {
    
    

    // 创建一个空的 map 容器,键为 string 类型,值为 int 类型
    map<string, int> myMap;   

    // 插入元素
    myMap.insert(pair<string, int>("Tom", 18));

    //容器的遍历
    cout << "遍历容器 :" << endl;
    for (map<string, int>::iterator it = myMap.begin(); it != myMap.end(); it++)
    {
    
    
        cout << it->first << "\t" << it->second << endl;
    }
    cout << "遍历结束" << endl;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

遍历容器 :
Tom 18
遍历结束
请按任意键继续. . .

在这里插入图片描述





二、map 容器插入结果处理



1、map#insert 函数返回值处理


map#insert 函数原型如下 , 其 返回值是 pair<iterator, bool> 类型 的 , 通过判定 pair 对组的第二个值来确定插入是否成功 ;


map#insert 函数原型 :

pair<iterator, bool> insert(const value_type& value);
  • 参数解析 :
    • 参数类型 : value_type 是 map 容器中存储的元素的类型 , 具体类型为 pair<const Key, T> 类型 , Key 是键的类型 , T 是值的类型 ;
    • 参数对象 : 传入的 value 对象 就是一个 pair 对组对象 , 是一个 键值对 元素 ;
  • 返回值解析 : 返回值类型为 pair<iterator, bool> , 也是一个 pair 对组对象 ;
    • 返回值对组的 第一个值是 iterator 迭代器 , 指向已经插入的 键值对 元素 ;
    • 返回值对组的 第二个值是 布尔值 , 表示插入是否成功 ,
      • 如果键 Key 已经存在 , 则插入失败 , 返回 false ;
      • 如果键 Key 不存在 , 则插入新元素 , 返回 true ;

下面的代码中 , map 容器的类型是 map<string, int> , 其迭代器类型是 map<string, int>::iterator , map#insert 函数的返回值是 迭代器类型 和 bool 值组成的键值对 , 该 map 容器对应的 insert 函数返回值是 pair<map<string, int>::iterator, bool> 类型 ;

    // 创建一个空的 map 容器,键为 string 类型,值为 int 类型
    map<string, int> myMap;   

	// 插入键值对 ("Tom", 18)
	myMap.insert(pair<string, int>("Tom", 18));

使用返回值接收上述 insert 函数插入 键值对元素 , 接收变量为 pair<map<string, int>::iterator, bool> insertRet ;

    // 创建一个空的 map 容器,键为 string 类型,值为 int 类型
    map<string, int> myMap;   

    // 插入键值对 ("Tom", 18)
    // 返回值类型为 pair<map<string, int>::iterator, bool>
    pair<map<string, int>::iterator, bool> insertRet = myMap.insert(pair<string, int>("Tom", 18));

上述返回的值类型为 pair<map<string, int>::iterator, bool> ,

使用 insertRet.first 可以访问 上述 键值对的 map<string, int>::iterator 迭代器值 ,

使用 *(insertRet.first) 可以访问到 map<string, int> 的键值对单个元素 pair<string, int> 对象 ,

使用 insertRet.first->first 可以访问 键值对元素的 键 Key ,

使用 insertRet.first->second 可以访问 键值对元素的 值 Value ;


2、代码示例


代码示例 :

#include "iostream"
using namespace std;
#include "map"
#include "string"


int main() {
    
    

    // 创建一个空的 map 容器,键为 string 类型,值为 int 类型
    map<string, int> myMap;   

    // 插入键值对 ("Tom", 18)
    // 返回值类型为 pair<map<string, int>::iterator, bool>
    pair<map<string, int>::iterator, bool> insertRet = myMap.insert(pair<string, int>("Tom", 18));

    // 判定插入是否成功
    if (insertRet.second != true) {
    
    
        cout << "(Tom, 18)插入失败" << endl;
    } else {
    
    
        cout << "(Tom, 18)插入成功 : " << insertRet.first->first << "\t" << insertRet.first->second << endl;
    }

    // 插入键值对 ("Tom", 12)
    insertRet = myMap.insert(make_pair("Tom", 12));

    // 判定插入是否成功
    if (insertRet.second != true) {
    
    
        cout << "(Tom, 12)插入失败" << endl;
    }
    else {
    
    
        cout << "(Tom, 12)插入成功 : " << insertRet.first->first << "\t" << insertRet.first->second << endl;
    }

    //容器的遍历
    cout << "遍历容器 :" << endl;
    for (map<string, int>::iterator it = myMap.begin(); it != myMap.end(); it++)
    {
    
    
        cout << it->first << "\t" << it->second << endl;
    }
    cout << "遍历结束" << endl;


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

执行结果 :

(Tom, 18)插入成功 : Tom 18
(Tom, 12)插入失败
遍历容器 :
Tom 18
遍历结束
请按任意键继续. . .

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/135326600