mardi 26 décembre 2017

what is the most elegant way of returning a lot of std::map<,>::iterator?

I am working on a car fleet program that has a container that contains a lot of cars

std::map<CarKey, Car> _cars;

I need to write a function/class that operate on a subset of the car objects in the _cars

Naively I can just iterate through _cars with a filter function

for(auto& p : _cars){
    //please note: I cannot get things done with one iteration, I have to iterate many times to get things done
    if (isOfInterest(p.second)){
        // do some thing
    }
}

the downside of such a solution is that is I am interested only in 10% of the cars, I will have to waste a lot time iterating

I am trying to find an elegant way to return all the iterators that I am interested

std::vector<std::map<CarKey, Car> :: iterator > getAllIntereted(_cars)

then I can simply iterate through the vector

I am not sure if this is a good approach. Maybe there is some design pattern that can be helpful?

Can anyone give any insights?

Thanks

Aucun commentaire:

Enregistrer un commentaire