most intelligent way to design a vector of pairs

Started by
4 comments, last by Zahlman 16 years, 6 months ago
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]
Advertisement

  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?

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

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]
Best regards, Omid
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.
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...


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.

This topic is closed to new replies.

Advertisement