STL map question
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
Hi Chris, I happen to be in the process of writing an STL-like bidirectional map, i.e. one for which you can lookup by key and by value. As my workload does not allow me to finish the thing, it'll be quite a while till I finally get to post it to CP. Nevertheless, if you're curious I can send to you what I have right now (it's already pretty functional, and beta testers are always great :) ). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
Chris Hambleton wrote: Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Nope. A map is normally a red-black tree implementation, so by swapping a key-value pair, you've essentially invalidated the location of that node in the tree. You can always remove the key-value pair first and then add the swapped values back in the tree. (More overhead, less memory usage of course.) Brandon
-
Hi Chris, I happen to be in the process of writing an STL-like bidirectional map, i.e. one for which you can lookup by key and by value. As my workload does not allow me to finish the thing, it'll be quite a while till I finally get to post it to CP. Nevertheless, if you're curious I can send to you what I have right now (it's already pretty functional, and beta testers are always great :) ). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I've been looking for this for several years now. At one point I was going to write one myself, but I'm not a stl guru. Please email me this if you can at drescherjm@hotmail.com Thanks, John M. Drescher
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
Humm, perhaps you could use a multimap, since insertion and remotion from the multimap doesn't invalidate the iterator(if I'm not mistaken here). Cheers, Joao Vaz A Programming Language is a tool that has profound influence on our thinking habits -The late giant Edsger Dijkstra 1930 - 2002 And if your dream is to care for your family, to put food on the table, to provide them with an education and a good home, then maybe suffering through an endless, pointless, boring job will seem to have purpose. And you will realize how even a rock can change the world, simply by remaining obstinately stationary. -
-
Hi Chris, I happen to be in the process of writing an STL-like bidirectional map, i.e. one for which you can lookup by key and by value. As my workload does not allow me to finish the thing, it'll be quite a while till I finally get to post it to CP. Nevertheless, if you're curious I can send to you what I have right now (it's already pretty functional, and beta testers are always great :) ). Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
I am looking forward to such an article. In my applications I have been using two maps depending on what keys I needed. I am also intereseted in becoming your beta tester of course :). Best regards, Alexandru Savescu
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
Well, until Joaquin bidirectional map comes out I suggest you use two maps and use one of them depending on what the keys are. This is what I have been doing. :) Best regards, Alexandru Savescu
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
Why not store as the value both they value and the key struct { int key; int value; } KeyValuePair; std::map<int, KeyValuePair> mymap; Then you can search through the map (although not as fast) by eithe the key or value. Todd Smith
-
Hi, I have an STL string pair (using a map) and every value is unique (just like the keys are unique). Sometimes, I need to "flip" the keys and the values, so the keys would become the values and the values become the keys. Is there a built-in function that would allow me to do this without having to create a new map and do this through a loop? Thanks!
You can always to this:
template <typename key_t, typename value_t>
class bidir_map {
typedef map<key_t, value_t> key_to_value_map;
typedef map<value_t, key_t> value_to_key_map;key\_to\_value\_map key\_map; value\_to\_key\_map value\_map; /\* Implement map-interface here which manipulate key\_map and value\_map accordingly during inserts and etc. \*/
};
Although, this may not be the most efficient implementation in terms of size. But if your collections is small, then this may be a cheap solution. -- Please state the nature of your medical emergency.
-
Why not store as the value both they value and the key struct { int key; int value; } KeyValuePair; std::map<int, KeyValuePair> mymap; Then you can search through the map (although not as fast) by eithe the key or value. Todd Smith
You can already do a linear search for values. Why add the key to the data set? -- Please state the nature of your medical emergency.