How to search for an element in an stl list?

后端 未结 5 998
南旧
南旧 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 19:42

    No, not directly in the std::list template itself. You can however use std::find algorithm like that:

    std::list my_list;
    //...
    int some_value = 12;
    std::list::iterator iter = std::find (my_list.begin(), my_list.end(), some_value);
    // now variable iter either represents valid iterator pointing to the found element,
    // or it will be equal to my_list.end()
    

提交回复
热议问题