most intelligent way to design a vector of pairs
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]
- Do you want to enforce that the ints be unique, or are you just observing it?
- How do you want to use this data?
- How often will you read from it?
- How often will you write to it?
- How often will you add or delete entries?
- When you read/write with it, will it be lots of scattered usage all over the place, or a set of consecutive records?
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]
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]
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.
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...
//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
Popular Topics
Advertisement