Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

ordered list problem

This topic is 3882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok I need to make a list with 2 bits on infomation. "name" and "distance" How can I have it such that it is ordered from smallest to largest by distance. eg so I can have (or something to get the same result): mylist[0] being the place with the sortest distances mylist[1] being the place with the next shortest distance etc I thought about useing a priority_queue but I'm not sure there how I'd go about making it smallest to greatest and how I'd store both the distance and the name in it.

Share this post


Link to post
Share on other sites
Advertisement
if you need to sort something easily consider writing simple bubble sort(ask google) function that would sort your array. i don`t know if there is something in c++ already, maybe someone else will tell you

Share this post


Link to post
Share on other sites
Depending on how this container is used std::multimap may be appropriate. This link describes std::multimap a little bit and has an example on use. So this may work:

std::multimap<int, std::string> m;
m.insert(pair<int, std::string>(20, "Detroit"));
m.insert(pair<int, std::string>(20, "Washington"));
m.insert(pair<int, std::string>(40, "Chicago"));
m.insert(pair<int, std::string>(10, "London"));

for (multimap<int, std::string>::iterator it = m.begin(); it != m.end(); ++it)
{
cout << " [" << (*it).first << ", " << (*it).second << "]" << endl;
}


I would read up on this container a little bit before really using it so you don't get bitten in case it doesn't do what you want.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!