什么是 Map 以及如何在 C++ 中使用 Map?
什么是地图? 我将如何在 C++ 中创建和使用一个?
What is a Map? How would I create and use one in C++?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
什么是地图? 我将如何在 C++ 中创建和使用一个?
What is a Map? How would I create and use one in C++?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
如果您的意思是
std::map
,它存储值对。 在每一对中,第一个值称为键,可用于快速查找关联的其他值。你可以写:
If you mean
std::map
, it stores pairs of values. In each pair, the first value is called the key, and can be used to quickly look up the associated other value.You can write:
什么是映射 - 它是一种保存成对相关值的数据结构。 每对由一个键和一个值组成。 映射中的每个键必须是唯一的,但不同的键可以具有相同的值。 映射通常用于缓存或实现查找表,很像字典(实际上这就是某些语言中映射的名称)。
至于如何在 C++ 中实现地图,简短的回答是 - 你不需要。 您使用 std::map 或其变体之一。
What is a map - it's a data structure that holds pairs of related values. Each pair consists of a key and a value. Every key in a map must be unique, but different keys can have the same values. Maps are often used for caching or implementing lookup tables, much like a dictionary (which is actually what maps are called in some languages).
As for how to implement a map in c++, short answer is - you don't. You use std::map or one of its variants.
Map是集合类型,它是在STL(标准模板库)中用C++实现的,这里是库文档的官方解释。
Map 是一个有序关联容器,它将 Key 类型的对象与 Data 类型的对象关联起来。 Map是一个Pair关联容器,意味着它的值类型是pair。 它也是一个唯一关联容器,这意味着没有两个元素具有相同的键。
Map 具有一个重要的属性,即向 Map 中插入新元素不会使指向现有元素的迭代器失效。 从映射中删除元素也不会使任何迭代器无效,当然,实际指向正在删除的元素的迭代器除外。
示例
这里是 STL 中 Map 类型的完整文档。
我希望这有帮助。
Map is collection type end it is implemented in C++ in the STL (Standard Template Library) end here is the official explanation from the library documentation.
Map is a Sorted Associative Container that associates objects of type Key with objects of type Data. Map is a Pair Associative Container, meaning that its value type is pair. It is also a Unique Associative Container, meaning that no two elements have the same key.
Map has the important property that inserting a new element into a map does not invalidate iterators that point to existing elements. Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased.
Example
Here is the complete documentation of the Map type in STL.
I hope this help.