Sign in to follow this  
thedustbustr

most intelligent way to design a vector of pairs

Recommended Posts

I want to have a vector of <string, int> pairs - the ints are unique. Is it better to store a vector of ints and map the ints to strings, or is it better to directly store a vector of pairs? std::vector<std::pair<std::string, int> > v; v.push_back(std::pair<std::string, int>("hello world",10)); std::map<int, std::string> m; std::vector<int> v; m[10] = "hello world"; v.push_back(10); [Edited by - thedustbustr on October 9, 2007 1:32:23 PM]

Share this post


Link to post
Share on other sites

  1. Do you want to enforce that the ints be unique, or are you just observing it?
  2. How do you want to use this data?
  3. How often will you read from it?
  4. How often will you write to it?
  5. How often will you add or delete entries?
  6. When you read/write with it, will it be lots of scattered usage all over the place, or a set of consecutive records?

Share this post


Link to post
Share on other sites
It depends on the usage. The last part of your example code is completely wrong though. For a std::map you would probably use something like

std::map<int, std::string> myMap;

myMap[0] = "This is zero";
myMap[10] = "This is ten";


Whereas if your ints are sequential, it might be beneficial to use a vector


std::vector<std::string> myVector;

myVector.push_back("This is zero");
myVector.push_back("This is one");

or

myVector.resize(100);

myVector[10] = "This is ten";
myVector[5] = "This is five";


Though the last example can turn out bad if you don't know what you are doing and start experimenting incorrectly.

It all depends on the usage.

EDIT: Too slow for the superpig [smile]

Share this post


Link to post
Share on other sites
The ints are really HWNDs, so I am observing that they are unique. I don't care if I enforce it or not. I just need to populate my vector with <HWND, caption> information, and I will use the vector to display a list of <HWND, caption> and let the user select a caption which is internally associated with a HWND.

Performance isn't really a concern as there are reasonable limits as to the number of windows displayed on one machine.

Reads and writes would be triggered by a user action, so its not stuff happening every frame or even every second. Typical read/write will be in bursts where the entire list is read or written.

Share this post


Link to post
Share on other sites
If I were doing this, I would probably do something like:

//hWnd is an existing window
std::map<HWND, std::string> windowCationtions;

windowCatptions[hWnd] = hWnd.title; // or whatever it is...


Share this post


Link to post
Share on other sites
Quote:
Original post by thedustbustr
The ints are really HWNDs, so I am observing that they are unique. I don't care if I enforce it or not. I just need to populate my vector with <HWND, caption> information, and I will use the vector to display a list of <HWND, caption> and let the user select a caption which is internally associated with a HWND.

Performance isn't really a concern as there are reasonable limits as to the number of windows displayed on one machine.

Reads and writes would be triggered by a user action, so its not stuff happening every frame or even every second. Typical read/write will be in bursts where the entire list is read or written.


The map makes the most sense, then. There is no need to hold the keys in a separate vector; you will likely be storing specific "interesting" keys in specific places anyway, and you can determine "all keys" by iterating over the map and examining the ->first of the iterator (remembering that a map basically stores std::pair<key, value> objects anyway). Conceptually, you are associating HWNDs with strings; the actual values and underlying-type of the HWNDs is only an implementation detail - use the map as Schmacker illustrates.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this