c++ mapiterator详解

发布时间:2023-05-18

一、mapiterator简介

mapiterator是C++中STL库中迭代器(iterator)的一种,是用于遍历map容器的迭代器。 map是一种基于关联性的容器,内部元素是按照一定的规则排列的,每个元素都有一个对应的关键字和值。map的元素是一对(key, value)的数值,其中key起到索引作用,value则是实际存储的数据。 通过mapiterator,我们可以对map中的每个元素进行遍历,获取其中的key和value的值,并进行相应的操作。

二、mapiterator使用方法

在使用mapiterator之前,我们需要先包含相应的头文件#include <map>,然后定义相应的map容器:

map<int, string> myMap; //定义一个以int为关键字类型,string为值类型的map容器

然后我们可以通过下面的代码插入元素,为示例展示能更加直观的反映出mapiterator的使用方法:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
myMap.insert(pair<int, string>(4, "Four"));
myMap.insert(pair<int, string>(5, "Five"));

定义完map容器后,我们可以使用mapiterator进行遍历,获取其中的元素。mapiterator有两种使用方法:begin和end。两者的区别在于begin返回的是map的第一个元素的迭代器,而end返回的是map的最后一个元素之后的迭代器。以下代码为mapiterator的基本使用方法:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
//使用begin和end方法对map容器进行遍历
map<int, string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); iter++)
{
    cout << iter->first << " " << iter->second << endl;
}

上述代码中,首先定义了一个map容器myMap,并向其中插入了3个元素。然后通过map<int, string>::iterator定义了一个名为iter的迭代器,使用for循环对myMap进行循环遍历,输出其中的元素。

三、mapiterator常用方法

1. operator*

operator*方法是mapiterator类中比较常用的方法,用于获取当前迭代器指向元素的value值。例如:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
map<int, string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); iter++)
{
    cout << iter->first << " " << *iter << endl;
}

上述代码中,使用迭代器实现了对myMap容器中元素的遍历,并通过operator*方法获取了当前迭代器指向元素的value值。

2. operator->

operator->方法是mapiterator类中另一个常用的方法,用于获取当前迭代器指向元素的key和value值。例如:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
map<int, string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); iter++)
{
    cout << iter->first << " " << iter->second << endl;
}

上述代码中,使用迭代器实现了对myMap容器中元素的遍历,并通过operator->方法获取了当前迭代器指向元素的key和value值。

3. operator++

operator++方法用于将迭代器指向下一个元素,例如:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
map<int, string>::iterator iter;
for (iter = myMap.begin(); iter != myMap.end(); iter++)
{
    cout << iter->first << " " << iter->second << endl;
    iter++;
    cout << iter->first << " " << iter->second << endl;
}

上述代码中,使用迭代器实现了对myMap容器中元素的遍历,并通过operator++方法将迭代器指向下一个元素。

4. operator--

operator--方法用于将迭代器指向上一个元素,例如:

map<int, string> myMap; //定义一个map容器
myMap.insert(pair<int, string>(1, "One"));
myMap.insert(pair<int, string>(2, "Two"));
myMap.insert(pair<int, string>(3, "Three"));
map<int, string>::iterator iter;
iter = myMap.end();
iter--; //将迭代器指向最后一个元素
for (; iter != myMap.begin(); iter--)
{
    cout << iter->first << " " << iter->second << endl;
}
cout << myMap.begin()->first << " " << myMap.begin()->second << endl;

上述代码中,首先将迭代器指向myMap的最后一个元素,并通过operator--方法将迭代器指向上一个元素,实现了对元素的倒序遍历。

四、mapiterator使用小结

通过以上的介绍,我们了解了mapiterator的基本使用方法和常用方法。在实际开发中,我们可以根据实际需求选择相应的方法进行map容器的遍历和操作,从而实现更高效、更灵活的代码编写。