c 语言中的find函数返回值,C ++ STL中的map find()函数

在本文中,我们将讨论C ++ STL中map::find()函数的工作,语法和示例。

什么是C ++ STL中的Map?

映射是关联容器,它有助于按特定顺序存储键值和映射值的组合所形成的元素。在映射容器中,数据始终在内部借助其关联的键进行排序。映射容器中的值通过其唯一键访问。

什么是map::find()?

map::find()是头文件下的函数。此函数返回一个迭代器,该迭代器指向要搜索的给定键的元素。

语法map_name.find(key_value k);

参数

该功能接受以下

参数

k:这是我们要从映射容器中搜索的键值

返回值

它返回一个指向与键k关联的元素的迭代器。

示例

输入项map newmap;

newmap[‘a’]

= 1;

newmap[‘b’] = 2;

newmap.find(b);

输出结果2

示例#include 

using namespace std;

int main() {

map TP_Map;

TP_Map.insert({3, 50});

TP_Map.insert({2, 30});

TP_Map.insert({1, 10});

TP_Map.insert({4, 70});

cout<

cout <

for (auto i = TP_Map.begin(); i!= TP_Map.end(); i++) {

cout <first <second <

}

//在位置找到映射值

auto var = TP_Map.find(1);

cout<first<second;

auto var_1 = TP_Map.find(2);

cout<first<second;

return 0;

}

输出结果TP Map is:

MAP_KEY    MAP_ELEMENT

1             10

2             30

3             50

4             70

Found element at position 1 is : 10

Found element at position 2 is : 30

示例#include 

using namespace std;

int main() {

map TP_Map;

TP_Map.insert({3, 50});

TP_Map.insert({2, 30});

TP_Map.insert({1, 10});

TP_Map.insert({4, 70});

cout<

cout <

for (auto i = TP_Map.find(2); i!= TP_Map.end(); i++) {

cout <first <second <

}

return 0;

}

输出结果TP Map is:

MAP_KEY    MAP_ELEMENT

2             30

3             50

4             70

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值