Thread: Is it possible to swap the key values and the mapped values around in a map?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    Having to just remember which swappedMap is for what is only a minor hassle, what makes it a genuine problem is that is also limits what you can do with them -- for example, if you pass the map in as a variable to a function, you will have to pass both maps.
    Not really. I don't see why you would need to pass both maps to a function.
    You would have to give me an example of what the function would be for before I even consider that possibility. I just can't imagine a scenario where you would pass one map to a function, then pass a swapped map of that map to the same function...
    Stuff like that is just a bad development practice -- you want to create options, not curtail them. At a certain point you will hit something this inflexibility will render impossible, meaning the programmer will have to create his/her own improptu interface, as above.
    I did create options:

    Code:
    const vector<string>& getEnumerationNames();
    const string& getNameOfEnum();
    const int& getNumOfEnumerations();
    const string& getEnumerationName(nameofenum enumeration);
    const map<string, unsigned int> getEnumerationsMap();
    Sorry, but I can just not imagine why you would need anything other than those functions for dealing with the enumeration names and their associated numerical values. swap() is a luxury actually. The main reason I added it in to begin with, was in case I needed to swap other maps than the enumeration maps. The other reason was for the rare times when someone might want to swap the enumerations map around, for whatever reason.
    Last edited by Programmer_P; 06-19-2010 at 12:53 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    I just can't imagine a scenario where you would pass one map to a function, then pass a swapped map of that map to the same function...
    Fair enough -- as I said after years of using map-like data structures, in reality it's very rare that you would need a swapped map at all. However, if you see a need for it, then it is strange that you would think someone would not want to write a function to use it.

    I did create options:
    This misses the point of object oriented programming, but nevermind.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    Fair enough -- as I said after years of using map-like data structures, in reality it's very rare that you would need a swapped map at all. However, if you see a need for it, then it is strange that you would think someone would not want to write a function to use it.
    I have already written a function: swap()

    This misses the point of object oriented programming, but nevermind.
    All those functions I mentioned (with the exception of swap(), which is global) are members of the generated enum's class.
    But admittedly, the generated class(es) are not OOP-oriented, since you would only need to create one object of each class. On the other hand though, I don't think OOP is the best answer for everything, nor was it the best answer for my generated file either.
    Last edited by Programmer_P; 06-19-2010 at 01:22 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed