The find member function in the container and the find method in the algorithm library

1. Find member functions
string, map, set and other containers have find member functions, so they can be used directly to find members in the container.

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

2. Find library method:
Containers such as list and vector do not have find member functions, so you must use the find method in the algorithm library to find members in the container.

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的元素"
}

to sum up:

1. Note that the two find parameters are different. The library method requires two iterators to indicate the starting and ending positions of the search.

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/106555933