什么是 Map 以及如何在 C++ 中使用 Map?

发布于 2024-07-25 02:15:55 字数 34 浏览 2 评论 0原文

什么是地图? 我将如何在 C++ 中创建和使用一个?

What is a Map? How would I create and use one in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落墨 2024-08-01 02:15:55

如果您的意思是 std::map,它存储值对。 在每一对中,第一个值称为键,可用于快速查找关联的其他值。

你可以写:

std::map<std::string, int> ages;
ages["Fred"] = 52;
ages["Sue"] = 31;

std::cout << "Fred's age is " << ages["Fred"] << std::endl;

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:

std::map<std::string, int> ages;
ages["Fred"] = 52;
ages["Sue"] = 31;

std::cout << "Fred's age is " << ages["Fred"] << std::endl;
空‖城人不在 2024-08-01 02:15:55

什么是映射 - 它是一种保存成对相关值的数据结构。 每对由一个键和一个值组成。 映射中的每个键必须是唯一的,但不同的键可以具有相同的值。 映射通常用于缓存或实现查找表,很像字典(实际上这就是某些语言中映射的名称)。

至于如何在 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.

贪了杯 2024-08-01 02:15:55

Map是集合类型,它是在STL(标准模板库)中用C++实现的,这里是库文档的官方解释。

Map 是一个有序关联容器,它将 Key 类型的对象与 Data 类型的对象关联起来。 Map是一个Pair关联容器,意味着它的值类型是pair。 它也是一个唯一关联容器,这意味着没有两个元素具有相同的键。
Map 具有一个重要的属性,即向 Map 中插入新元素不会使指向现有元素的迭代器失效。 从映射中删除元素也不会使任何迭代器无效,当然,实际指向正在删除的元素的迭代器除外。

示例

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main()
{
  map<const char*, int, ltstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "june -> " << months["june"] << endl;
  map<const char*, int, ltstr>::iterator cur  = months.find("june");
  map<const char*, int, ltstr>::iterator prev = cur;
  map<const char*, int, ltstr>::iterator next = cur;    
  ++next;
  --prev;
  cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
  cout << "Next (in alphabetical order) is " << (*next).first << endl;
}

这里是 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

struct ltstr
{
  bool operator()(const char* s1, const char* s2) const
  {
    return strcmp(s1, s2) < 0;
  }
};

int main()
{
  map<const char*, int, ltstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "june -> " << months["june"] << endl;
  map<const char*, int, ltstr>::iterator cur  = months.find("june");
  map<const char*, int, ltstr>::iterator prev = cur;
  map<const char*, int, ltstr>::iterator next = cur;    
  ++next;
  --prev;
  cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
  cout << "Next (in alphabetical order) is " << (*next).first << endl;
}

Here is the complete documentation of the Map type in STL.

I hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文