在这篇文章中,我们介绍了C++ STL中map::begin和map::end的概念,并附有C++代码示例。
内容列表
以下是本文将讨论的内容列表
- 简介
maps - 地图的用途
map - 访问a的元素
map - 在a中使用
begin()函数map - 在地图中使用
end()函数
地图简介
地图是c++ 中的特殊数据结构,它以键值对的形式存储数据。它们被称为map ,因为这个数据结构中的每个键都被映射到其相应的值。地图使用二进制搜索树来存储这些键、值对。它使用pair 数据结构来将键映射到其相应的值。
地图的用途
地图可以用于许多不同的目的,例如存储任何类型的特定对象的属性,从巨大的数据集合中找到任何对象,存储有序的数据,以及更多。
地图的例子
// Example to demostrate basic use of a map
#include <iostream>
#include <map>
using std::map;
using std::cout;
int main () {
map<char, int> example;
example['z'] = 11;
example['x'] = 12;
example['y'] = 13;
cout << "Printing the contents of a map:\n";
for (auto iter:example) {
cout << iter.first << ": " << iter.second << '\n';
}
return 0;
}
上面的代码将发出以下输出:
Printing the contents of a map:
x: 12
y: 13
z: 11
注意:从上面的例子中我们可以看到,c++中的地图是按照键的顺序来对存储的数据进行排序的。
访问地图的元素
我们已经研究了在地图中作为键值对插入元素的问题,但是如何访问地图中的单个元素呢?为了实现这一点,我们可以使用索引,即提供一个键作为索引,像这样:
cout << element['x'] << '\n';
如果存在一个与给定索引值相同的键,那么存储在该键上的值将被返回。
另一种方法是使用 迭代器,我们将在今天的主题中学习。
地图中的迭代器函数
迭代器函数是用来返回任何集合类型数据结构的迭代器的函数。这些函数使我们能够轻松地浏览这些数据结构。它们还为我们提供了在任何此类数据结构中寻找任何特定数据的能力。
一个iterator ,只是一个指向集合数据结构中特定位置的限制性类型的指针。迭代器只存在于集合中,而不是单个数据类型。
在一个map中,迭代器指向一个特定的pair 数据结构,存储在 二进制搜索树,在地图中。
注意:一个迭代器只是一个指针,如何定位和返回取决于数据结构的类型。
c++ 中的地图提供了多个迭代器函数,我们今天要讨论的是:
map::begin()函数。map::end()函数
map::begin() 函数
map::begin() 顾名思义,它返回一个指向地图开头的迭代器。这允许访问地图中的第一个元素,并提供一个起点来遍历整个地图。
例子:
//Includes are not shown in this example
int main () {
map<int, int> example;
example[75] = 343;
example[42] = 117;
example[95] = 12;
cout << "Map begins at key: " << example.begin()->first << '\n';
cout << "Value stored at beginning of map: "
<< example.begin()->second
<< '\n';
return 0;
}
上面的代码将发出以下输出:
Map begins at key: 42
Value stored at beginning of map: 117
我们可以用这个函数来遍历整个地图,使用以下方法方案:
int main () {
// --Map initialization and insertion of values is same as the previous example-- //
for (auto iter = example.begin(); iter != example.end(); i++) {
cout << "Key: " << iter->first << " value: " << iter->second << '\n';
}
return 0;
}
example.begin() 返回指向地图起点的迭代器,然后将其分配给变量iter 。example.end() 返回指向地图终点的另一个迭代器,我们将在文章后面了解它。因为,iter 是一个迭代器,它也可以像其他指针一样,这意味着它可以像其他指针一样被递增和递减。因此,我们开始一个for循环,iter 最初被设置为example.begin ,然后被递增,直到它达到example.end() ,对于iter 的每一个值,我们都要打印它的值。
由于iter 是一个指向地图中特定的pair 的指针,我们将不得不使用-> 操作符来引用存储在pair 的值。
map::end() 函数
map::end() 返回一个到地图末端的迭代器。它告诉我们,地图不再包含任何值。在我们需要在地图中找到一个元素的情况下,它很有用。如果一个特定的元素没有被找到,那么我们可以安全地返回由 返回的迭代器。map::end()
例子:
对于前面提到的例子,如果我们像这样调用end函数。
int main () {
// --Map initialization and insertion of values is same as the previous example-- //
cout << "Map ends at key: " << example.end()->first << '\n';
cout << "Value stored at ending of map: "
<< example.end()->second
<< '\n';
return 0;
}
我们会收到下面的输出:
Map ends at key: 3
Value stored at ending of map: 0
我们可以注意到,在给定的迭代器位置的键值对中的任何一个元素都不存在于我们的地图中。这是因为map::end() 并没有返回指向地图最后一个元素的迭代器,而是指向给定地图之外的一个位置。
这在我们需要在给定地图中找到一个元素的情况下是非常有帮助的。
例子:
int main () {
// --Map initialization and insertion of values is same as the previous example-- //
// The key exists
auto key_iterator = example.find(343);
if (key_iterator != example.end()) {
cout << "Key found with value: " key_iterator->second << '\n';
}
else {
cout << "Key not found\n";
}
// The key does not exist
key_iterator = example.find(900);
if (key_iterator != example.end()) {
cout << "Key found with value: " key_iterator->second << '\n';
}
else {
cout << "Key not found\n";
}
return 0;
}
这产生的输出结果是这样的:
Key found with value: 343
Key not found: 3
如果map::end() 函数返回指向地图中存储的最后一个元素的迭代器,那么就很难检查map::find() 返回的元素是没有找到还是只是地图的最后一个元素。
那么,如何找到地图的最后一个元素呢?幸运的是,我们有一种方法可以使用map::end() 来获得地图的最后一个元素。
让我们看看它是如何工作的
int main () {
// --Map initialization and insertion of values is same as the previous example-- //
// Print all the elements of the map
for (auto iter:example) {
cout << "Element with key: " << iter.first
<< " is: " << iter.second
<< '\n';
}
// Print the last element of the map using `map::end()`
cout << "Last element of the map is: "
<< (--example.end())->second
<< '\n';
return 0;
}
以下代码的输出是:
Element with key: 42 is: 117
Element with key: 75 is: 343
Element with key: 95 is: 12
Last element of the map is: 12
让我们分析一下我们在这里做了什么:
- 首先,我们打印整个地图,通过基本的遍历知道什么应该是地图的最后一个元素,这样做是为了检查我们实际逻辑的有效性。
- 现在让我们分析一下这部分代码,它使用
map::end()来打印最后一个元素:
cout << "Last element of the map is: "
<< (--example.end())->second
<< '\n';
- 我们在这里看到一些有趣的东西,什么是
(--example.end())->second?
正如我们前面所学到的,迭代器只是一个受限的指针,它指向集合数据结构中的一个特定位置。这意味着如果我用1 递减迭代器,我将能够得到存储在我们手中的迭代器之前的元素的位置。我们知道map::end() ,返回的迭代器比最后一个元素的位置大1 ,我们将指针向后移动一步,访问存储在地图中的最后一个元素。
结论
c++ 或任何其他语言中的地图是存储数据的一种出色方式。它们提供了以键值对的形式存储数据的能力,这在跨多个服务(如API)发送数据时非常有用。我们可以使用合理的键来作为索引,而不是通过一些编号的索引来访问值,从而使代码更加可读和实用。map::begin() 和map::end() 函数使地图的使用更加容易,因为它们允许我们在地图中轻松地进行导航和搜索。尽管它们只是地图中众多此类功能中的两个,但它们仍然提供了足够的基本功能,可以让我们开始使用。
通过OpenGenus的这篇文章,你一定对C++ STL中的map::begin和map::end有了完整的概念。