samedi 25 mai 2019

std::find_if map to object

Should I use example 1 or example 2 for map pointer from find_if to object Which one is the best?

struct test {
 INT id
}

std::vector<std::shared_ptr<test>> vec;

int ID = 75;

auto obj = std::find_if(vec.begin(), vec.end(), [&ID](std::shared_ptr<test>& r){
 return r->id == ID;
});

if ( obj != vec.end() ) {
 // example 1
 std::shared_ptr<test> example1 = (*obj);

 // example 2 by reference
 std::shared_ptr<test>& example2 = (*obj);
}

Aucun commentaire:

Enregistrer un commentaire