Skip to main content
GameDev.net gamedev.net
🔒 Locked

Algorithm for best choice - with pointer

Started by algumacoisaqualquer Jan 23, 2007 at 12:49 PM 0 replies 950+ views
Original Post
algumacoisaqualquer
algumacoisaqualquer
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:

//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);
	



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:

//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;
		}
	}



Isn't there a better way to do this? Thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.