迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中检查 map 中是否存在某个键值

作者:迹忆客 最近更新:2024/01/02 浏览次数:

本文将介绍如何在 C++ 中检查 map 中是否存在某个键。


使用 std::map::find 函数检查 C++ map 中是否存在某个键

std::map 容器是一个键值对的关联数据结构,按顺序存储,每个元素都有一个唯一的键。另一方面,STL 还提供了一个名为 std::unordered_map 的同一个容器的未排序版本。这两个容器都支持下面描述的关键字搜索方法。

findstd::map 容器的内置函数之一,它采用对应键值的单个参数进行搜索。该函数返回具有给定键值的元素的迭代器,否则返回尾后迭代器。在以下示例中,我们初始化 std::pair<string, string> 类型的 map,然后从传递给 find 函数的用户输入中获取键值。示例程序将肯定字符串输出到 cout 流。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  string key_to_find;
  std::map<string, string> lang_map = {{
                                           "j",
                                           "Julia",
                                       },
                                       {
                                           "p",
                                           "Python",
                                       },
                                       {
                                           "m",
                                           "MATLAB",
                                       },
                                       {
                                           "o",
                                           "Octave",
                                       },
                                       {
                                           "s",
                                           "Scala",
                                       },
                                       {
                                           "l",
                                           "Lua",
                                       }};

  for (const auto& [key, value] : lang_map) {
    cout << key << " : " << value << endl;
  }

  cout << "Enter the key to search for: ";
  cin >> key_to_find;

  if (lang_map.find(key_to_find) != lang_map.end()) {
    cout << "Key Exists!" << endl;
  }

  return EXIT_SUCCESS;
}

输出:

j : Julia
l : Lua
m : MATLAB
o : Octave
p : Python
s : Scala
Enter the key to search for: l
Key Exists!

使用 std::map::count 函数检查 C++ map 中是否存在某个键值

或者,可以利用 std::map 容器的 count 内置函数来检查给定的键是否存在于 map 对象中。请注意,count 函数检索具有给定键值的元素的数量。如果没有找到键为 0 的元素,则返回值。因此,当给定的键存在于 map 对象中时,我们可以使用 count 函数调用作为 if 条件来输出确认字符串。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  string key_to_find;
  std::map<string, string> lang_map = {{
                                           "j",
                                           "Julia",
                                       },
                                       {
                                           "p",
                                           "Python",
                                       },
                                       {
                                           "m",
                                           "MATLAB",
                                       },
                                       {
                                           "o",
                                           "Octave",
                                       },
                                       {
                                           "s",
                                           "Scala",
                                       },
                                       {
                                           "l",
                                           "Lua",
                                       }};

  cout << "Enter the key to search for: ";
  cin >> key_to_find;

  if (lang_map.count(key_to_find)) {
    cout << "Key Exists!" << endl;
  }

  return EXIT_SUCCESS;
}

输出:

Enter the key to search for: l
Key Exists!

使用 std::map::contains 函数检查 C++ map 中是否存在某个键值

contains 是另一个内置函数,可用于查找键是否存在于 map 中。如果具有给定键的元素存在于对象中,则此函数返回一个布尔值。请注意,本文中列出的所有三个函数都具有对数复杂度。

#include <iostream>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::map;
using std::string;

int main() {
  string key_to_find;
  std::map<string, string> lang_map = {{
                                           "j",
                                           "Julia",
                                       },
                                       {
                                           "p",
                                           "Python",
                                       },
                                       {
                                           "m",
                                           "MATLAB",
                                       },
                                       {
                                           "o",
                                           "Octave",
                                       },
                                       {
                                           "s",
                                           "Scala",
                                       },
                                       {
                                           "l",
                                           "Lua",
                                       }};

  cout << "Enter the key to search for: ";
  cin >> key_to_find;

  if (lang_map.contains(key_to_find)) {
    cout << "Key Exists!" << endl;
  }

  return EXIT_SUCCESS;
}

输出:

Enter the key to search for: l
Key Exists!

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便