美文网首页
c++map判断键值对是否存在

c++map判断键值对是否存在

作者: 一路向后 | 来源:发表于2021-03-22 22:58 被阅读0次

1.源码实现

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
        map<int, string> mapStudent;
        map<int, string>::iterator it;

        mapStudent.insert(pair<int, string>(1, "student one"));
        mapStudent.insert(pair<int, string>(2, "student two"));
        mapStudent.insert(pair<int, string>(3, "student three"));

        it = mapStudent.find(2);

        if(it != mapStudent.end())
        {
                cout << "Find, the value is " << it->second << endl;
        }
        else
        {
                cout << "Do not find" << endl;
        }

        return 0;
}

2.编译源码

$ g++ -o example example.cpp

3.运行及其结果

$ ./example
Find, the value is student two

相关文章

  • c++map判断键值对是否存在

    1.源码实现 2.编译源码 3.运行及其结果

  • map

    使用string做输入比较好。 打印map 判断某个键值是否存在:

  • Map和Set

    Map:一组键值对的结构,具有极快查找速度有has(判断是否有该键值对)get(得到对应键值)set(设置键值对)...

  • 字典Dict 判断指定键值是否存在

    初始化数组 一。通过字典对象的方法 has_key 判断 二。通过 in keys()的方法

  • 01.03 笔记-字典的内建函数

    比较运算 == ,!= 注意:判断两个字典是否相等,只看键值对是否一样,不管键值对的顺序; 字典不支持比较大小 >...

  • 19-01-03字典运用

    1.比较运算 ==, !=注意:判断两个字典是否相等,只看键值对是否一样,不管键值对的顺序;字典不支持>和<符号 ...

  • 字典的方法

    1.比较运算 ==, !=注意:判断两个字典是否相等,只看键值对是否一样,不管键值对的顺序;字典不支持>和<符号 ...

  • 【perl】perl哈希(三)——哈希的函数

    exist函数 判断hash中是否存在对应的键值,返回0不存在,1存在 如果存在,但值为空,返回也是1 each函...

  • PHP中empty、isset和array_key_exists

    文章参考: 1.PHP判断键值数组是否存在,使用empty或isset或array_key_exists2.arr...

  • php常用方法

    数组 array_key_exists(array) 简介 判断数组内是否存在某个键值。 用法 两个参数 key...

网友评论

      本文标题:c++map判断键值对是否存在

      本文链接:https://www.haomeiwen.com/subject/ijxxhltx.html