Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. STL map question

STL map question

Scheduled Pinned Locked Moved ATL / WTL / STL
questionc++
10 Posts 8 Posters 63 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Hambleton
    wrote on last edited by
    #1

    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!

    J C J A T 6 Replies Last reply
    0
    • C Chris Hambleton

      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!

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      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

      J A 2 Replies Last reply
      0
      • C Chris Hambleton

        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!

        C Offline
        C Offline
        CodeGuy
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • J Joaquin M Lopez Munoz

          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

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Chris Hambleton

            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!

            J Offline
            J Offline
            Joao Vaz
            wrote on last edited by
            #5

            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. -

            1 Reply Last reply
            0
            • J Joaquin M Lopez Munoz

              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

              A Offline
              A Offline
              Alexandru Savescu
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • C Chris Hambleton

                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!

                A Offline
                A Offline
                Alexandru Savescu
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • C Chris Hambleton

                  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!

                  T Offline
                  T Offline
                  Todd Smith
                  wrote on last edited by
                  #8

                  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

                  J 1 Reply Last reply
                  0
                  • C Chris Hambleton

                    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!

                    J Offline
                    J Offline
                    Jorgen Sigvardsson
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • T Todd Smith

                      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

                      J Offline
                      J Offline
                      Jorgen Sigvardsson
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups