Map中,find函数用来定位数据出现位置,当含有该数据,即查找成功时,返回数据所在未知的迭代器,

如果查找失败,则返回end()函数所在的迭代器,因此用是否等于end来判断是否查找成功。

程序示例:

#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    map<string , int> m;
    m.insert(pair<string, int>("Hello", 1));
    m.insert(pair<string, int>("world", 2));
    map<string , int>::iterator it;
    it = m.find("Hello");
    if(it!=m.end())
    {
        cout<<it->first<<" Find success!"<<endl;
    }
    else
    {
        cout<<"Find failed!"<<endl;
    }
    return 0;
}

 

posted on 2019-01-28 15:50  曹婷婷  阅读(6921)  评论(0编辑  收藏  举报