map中[]运算符和find的区别

此文章转载自:https://blog.csdn.net/weixin_39831546/article/details/78314476

map的下标运算符[]的作用是:将关键码作为下标去执行查找,并返回对应的值;如果不存在这个关键码,就将一个具有该关键码和值类型的默认值的项插入这个map。

map<string,string> m;
m["first"] = "hello";
m["second"] = "world";

cout<<"[]查找之前"<<endl;
map<string,string>::iterator it = m.begin();
for(;it!=m.end();++it)
{
    cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
cout<<"查找之后"<<endl;
if(m["third"] == "test")
{
    m["third"] == "testresult";
}
it = m.begin();
for(;it!=m.end();++it)
{
        cout<<it->first<<" "<<it->second<<endl;
}

//operator []的源码
mapped_type& operator[](const key_type& _Keyval)
{ // find element matching _Keyval or insert with default mapped
iterator _Where = this->lower_bound(_Keyval);
if (_Where == this->end()
|| this->comp(_Keyval, this->_Key(_Where._Mynode())))
_Where = this->insert(_Where,
value_type(_Keyval, mapped_type()));
return ((*_Where).second);
}

map的find函数:用关键码执行查找,找到了返回该位置的迭代器;如果不存在这个关键码,就返回尾迭代器。

map<string,string> f;
f.insert(make_pair("first","hello"));
f.insert(make_pair("second","world"));
cout<<"find查找之前"<<endl;
map<string,string>::iterator it = f.find("third");
if(it != f.end())
{
    cout<<it->second<<endl;
}
else
{
    cout<<"没找到"<<endl;
}
cout<<endl;
cout<<"find查找之后"<<endl;
it = f.begin();
for(;it!=f.end();++it)
{
    cout<<it->first<<" "<<it->second<<endl;
}

这里写图片描述

//再看看find的源码
const_iterator find(const key_type& _Keyval) const
{ // find an element in nonmutable sequence that matches _Keyval
const_iterator _Where = lower_bound(_Keyval);
return (_Where == end()
|| _DEBUG_LT_PRED(this->comp,
_Keyval, _Key(_Where._Mynode()))
? end() : _Where);
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++map是一种关联容器,它将键和值一一对应,可以通过键快速查找值。map的find函数用于查找指定键是否存在,并返回对应的迭代器。 以下是map的find函数的语法: ```c++ map_name.find(key); ``` 其map_name是map的名称,key是要查找的键。find函数的返回值是一个迭代器,如果找到了指定的键,则返回指向该键值对的迭代器;如果没有找到,则返回指向map末尾的迭代器。 以下是一个示例代码,演示如何使用map的find函数: ```c++ #include <iostream> #include <map> using namespace std; int main() { map<string, int> phone_book; phone_book["Alice"] = 123456; phone_book["Bob"] = 234567; phone_book["Charlie"] = 345678; // 查找键为 "Bob" 的值 map<string, int>::iterator it = phone_book.find("Bob"); if (it != phone_book.end()) { cout << "Bob's phone number is: " << it->second << endl; } else { cout << "Bob's phone number not found." << endl; } // 查找键为 "David" 的值 it = phone_book.find("David"); if (it != phone_book.end()) { cout << "David's phone number is: " << it->second << endl; } else { cout << "David's phone number not found." << endl; } return 0; } ``` 输出结果为: ``` Bob's phone number is: 234567 David's phone number not found. ``` 在这个示例代码,首先创建了一个名为phone_book的map,用来存储人名和电话号码。然后通过[]运算符将几个名字和电话号码添加到map。 接着使用find函数查找键为"Bob"和"David"的电话号码。对于"Bob",找到了对应的键值对,输出了电话号码;对于"David",没有找到对应的键值对,输出了提示信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值