How to use std::find/std::find_if with a vector of custom class objects?

前端 未结 6 680
故里飘歌
故里飘歌 2020-11-29 04:09

I have a class representing a user called Nick and I want to use std::find_if on it, where I want to find if the userlist vector has an object incl

6条回答
  •  攒了一身酷
    2020-11-29 04:30

    If you are using C++0X you can use a simple lambda expression

    std::string username = "Nicholas";    
    std::find_if(userlist.begin(), userlist.end(), [username](Nick const& n){
        return n.username == username;
    })
    

提交回复
热议问题