容器中的find成员函数与algorithm库中的find方法

1、find成员函数
string、map、set等容器有find成员函数,因此可以直接用来查找容器中的成员。

map<int,int> myMap;
myMap[1] = 3;
myMap[4] = 6;
auto ptr = myMap.find(1);
if(ptr == myMap.end()){
    
    
	cout<<"容器中不存在键为1的元素"
}

2、find库方法:
list、vector等容器没有find成员函数,因此必须借助algorithm库中的find方法查找容器中的成员。

list<int> myList;
myList.push_back(2);
myList.push_back(5);
myList.push_back(3);
auto ptr = find(myList.begin(),myList.end(),3);
if(ptr == myList.end()){
    
    
	cout<<"容器中不存在为3的元素"
}

总结:

1、注意两个find的参数不同,库方法需要两个迭代器表示搜索的起止位置。

猜你喜欢

转载自blog.csdn.net/qq_33726635/article/details/106555933