Original Post
Ok, first of all, this is C++. Basically, I have a class called c_city, that has a vector member of pointers to my class c_travel. Now, I want to pick the cheapest travel, but also a travel for a city that I haven't visited yet. The code I have so far is: The problem is that, when I first create cheapest_travel, I assign a random value for it, but I won't check if that travel is ok (I mean, if that travel ends up in a allready visited city). Any ideas on how to do that? I'm thinking about going with a for loop, something like this: Isn't there a better way to do this? Thanks!
//This is actually inside another class. visited_cities is a vector of pointers to c_city.
c_travel* cheapest_travel = &visited_cities.back()->travels.back();
for(size_t i = 0; i < visited_cities.back()->travels.size(); ++i)
{
if((visited_cities.back()->travels.price < cheapest_travel->price)
&& (!city_already_visited(visited_cities.back()->travels.end)))
{
cheapest_travel = &visited_cities.back()->travels;
}
}
visited_cities.push_back(cheapest_travel->end);
//EDIT: I tried to fix it, but this is not really working
for(size_t i = 0; i < visited_cities.back()->travels.size(); ++i)
{
if(!city_already_visited(visited_cities.back()->travels.end))
{
cheapest_travel = &visited_cities->travels;
break;
}
}