C++中如何查询map中是否存在某个元素

//1 利用count
#include <unordered_map>
unordered_map<string, int> dist;
dist.count(x) == 1;//x元素存在
dist.count(x) == 0;//x元素不存在
//2 利用find
dist.find(x) != dist.end();//x元素存在
dist.find(x) == dist.end();//x元素不存在
//3 利用迭代器遍历
unordered_map<string, int>::iterator it = dist.begin();
bool IsExist = false;
while(it != dist.end())
{
	if(it->first == x)
	{
		IsExist = true;
		break;
	}
	it++;
}	
IsExist == true;//x元素存在
IsExist == false;//x元素不存在

示例代码,

#include <iostream>
#include <unordered_map>

using namespace std;

void method1(unordered_map<string, int> dist, string x)
{
    if(dist.count(x) == 1)
        cout << "键值对dist中存在" << x << "!" << endl;
    else
        cout << "键值对dist中不存在" << x << "!" << endl;
}

void method2(unordered_map<string, int> dist, string x)
{
    if(dist.find(x) != dist.end())
        cout << "键值对dist中存在" << x << "!" << endl;
    else
        cout << "键值对dist中不存在" << x << "!" << endl;
}

void method3(unordered_map<string, int> dist, string x)
{
    unordered_map<string, int>::iterator it = dist.begin();
    bool IsExist = false;
    while(it != dist.end())
    {
        if(it->first == x)
        {
            IsExist = true;
            break;
        }
        it++;
    }
    if(IsExist)
       cout << "键值对dist中存在" << x << "!" << endl;
    else
       cout << "键值对dist中不存在" << x << "!" << endl;
}


int main()
{

    unordered_map<string, int> dist;
    dist["Chinese"] = 1;
    dist["math"] = 2;
    dist["English"] = 3;
    dist["physics"] = 4;
    dist["chemistry"] = 5;
    dist["biology"] = 6;

    //1 利用count
    cout << "1利用count: " << endl;
    method1(dist, "Chinese");
    method1(dist, "history");

    //2 利用find
    cout << "2利用find: " << endl;
    method2(dist, "Chinese");
    method2(dist, "history");

    //3 利用迭代器遍历
    cout << "3利用迭代器遍历: " << endl;
    method3(dist, "Chinese");
    method3(dist, "history");

    return 0;
}

输出为,

1利用count: 
键值对dist中存在Chinese!
键值对dist中不存在history!
2利用find: 
键值对dist中存在Chinese!
键值对dist中不存在history!
3利用迭代器遍历: 
键值对dist中存在Chinese!
键值对dist中不存在history!
  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C++ ,判断 std::map 是否包含某个键或值可以使用 find() 或 count() 成员函数。 1. 使用 find() 函数 find() 函数接受一个键作为参数,返回一个指向包含该键的元素的迭代器。如果 map 不存在该键,则返回指向 map 末尾的迭代器。因此,可以通过判断返回的迭代器是否等于 map 的 end() 迭代器来判断 map 是否包含该键。 下面是一个使用 find() 函数判断 map 是否包含某个键的示例: ```c++ #include <iostream> #include <map> int main() { std::map<std::string, int> my_map = {{"one", 1}, {"two", 2}, {"three", 3}}; // 判断 map 是否包含 "two" 键 if (my_map.find("two") != my_map.end()) { std::cout << "my_map contains key 'two'\n"; } // 判断 map 是否包含 "four" 键 if (my_map.find("four") == my_map.end()) { std::cout << "my_map does not contain key 'four'\n"; } return 0; } ``` 2. 使用 count() 函数 count() 函数接受一个键作为参数,返回该键在 map 出现的次数,因为 map 每个键最多出现一次,所以如果 count() 函数返回值为 1,则说明 map 包含该键;如果返回值为 0,则说明 map 不包含该键。 下面是一个使用 count() 函数判断 map 是否包含某个键的示例: ```c++ #include <iostream> #include <map> int main() { std::map<std::string, int> my_map = {{"one", 1}, {"two", 2}, {"three", 3}}; // 判断 map 是否包含 "two" 键 if (my_map.count("two") == 1) { std::cout << "my_map contains key 'two'\n"; } // 判断 map 是否包含 "four" 键 if (my_map.count("four") == 0) { std::cout << "my_map does not contain key 'four'\n"; } return 0; } ``` 需要注意的是,如果需要判断 map 是否包含某个值,需要遍历整个 map 才能进行判断。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值