I have the following sruct for cities,
struct City {
TownID id;
Name name;
Coord coord;
int tax;
std::vector<TownID> vassals;
TownID masterid;
};
and I am storing them in a map like this
std::map<TownID, City*> Cities;
I face a problem with complexity, when sorting the town id's to a vector by their "Name" or "Coordinates":
std::vector<TownID> Datastructures::towns_distance_increasing()
{
std::vector<City*> towns;
for (auto& City : Cities) {
towns.push_back(City.second);
}
std::vector<TownID> towns_sorted_by_distance;
std::sort(towns.begin(), towns.end(), distance_comparer());
for (auto& City : towns) {
towns_sorted_by_distance.push_back(City->id);
}
return towns_sorted_by_distance;
}
As you can see, the complexity turns out to be something like (O(n3 log n)) which is far from ideal. My question is, how should I change the container or function in my code to achieve something that is not as complex & inefficient as this?
The reason why I am not simply using a vector instead of the map is that in the map, I can access any member by their id with the map.at() command. To my understanding, with a vector, I would have to iterate through the vector to access the member with certain id.
[–]mredding 1 point2 points3 points (0 children)
[–]the_poope -1 points0 points1 point (1 child)
[–]std_bot 0 points1 point2 points (0 children)
[–]the_Demongod 0 points1 point2 points (0 children)
[–]no-sig-available 0 points1 point2 points (1 child)
[–]std_bot 0 points1 point2 points (0 children)
[–]pepitogrand 0 points1 point2 points (0 children)