How to search for an element in an stl list?

后端 未结 5 988
南旧
南旧 2020-12-04 19:13

Is there a find() function for list as there was in vector?

Is there a way to do that in list?

5条回答
  •  抹茶落季
    2020-12-04 20:00

    No, find() method is not a member of std::list. Instead, use std::find from

        std :: list < int > l;
        std :: list < int > :: iterator pos;
    
        l.push_back(1);
        l.push_back(2);
        l.push_back(3);
        l.push_back(4);
        l.push_back(5);
        l.push_back(6);
    
        int elem = 3;   
        pos = find(l.begin() , l.end() , elem);
        if(pos != l.end() )
            std :: cout << "Element is present. "<

提交回复
热议问题