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

  1. #76
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    You, if you truly intend to use a map like that. Saying that the current methods "kind of sucked" is bogus when the map is simply not designed to do that. You are using the wrong container for the job, then blaming the container. That sucks, on your part.
    No, I'm not. I needed a container which stores two kinds of values, and allows access to one kind of value with the other kind of value, and the map (or multimap) fits that bill.
    Maps and multimaps are not designed for efficient access by sequential index. They are associative containers designed for efficient access by key value. They are not designed for efficient access by mapped value.
    The bolded statements should not be exclusive to one another. Ideally, a map should allow efficient access by sequential index of the the current key value, i.e. so that you increment the index iterator to access the next key value of the map, and hence, also the mapped value. I was complaining mostly about the syntax necessary to achieve that goal in a map or multimap.
    For example:
    Code:
    template<typename IT, typename T>
    bool value_exists(IT begin, IT end, const T& value)
    {
        for (; begin != end; ++begin)
        {
            if (begin->second == value)
            {
                return true;
            }
        }
        return false;
    }
    
    #include <map>
    #include <string>
    
    int main()
    {
        std::map<std::string, int> x;
        // ...
        if (value_exists(x.begin(), x.end(), 123))
        {
            // ...
        }
    }
    That said, instead of implementing an algorithm, I could have implemented a predicate and used std::find_if, e.g.,
    Code:
    template<typename Pair>
    class CompareSecond
    {
    public:
        explicit CompareSecond(const typename Pair::second_type& x) : x_(x) {}
    
        bool operator()(const Pair& y) const
        {
            return y.second == x_;
        }
    private:
        typename Pair::second_type x_;
    };
    
    #include <algorithm>
    
    template<typename IT, typename T>
    bool value_exists(IT begin, IT end, const T& value)
    {
        return std::find_if(begin, end,
            CompareSecond<typename IT::value_type>(value)) != end;
    }
    
    #include <map>
    #include <string>
    
    int main()
    {
        std::map<std::string, int> x;
        // ...
        if (value_exists(x.begin(), x.end(), 123))
        {
            // ...
        }
    }
    Ok, but using that method requires writing your own function for using a generic-type of iterator for a map. There is nothing for that already provided by std::map or std::multimap.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #77
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    You, if you truly intend to use a map like that. Saying that the current methods "kind of sucked" is bogus when the map is simply not designed to do that. You are using the wrong container for the job, then blaming the container. That sucks, on your part.
    @Programmer_P: I'm just going to re-iterate the point I tried very hard to make earlier, that this should be a class containing a map, not just a map. Simple. Or you can keep digging this hole deeper.

    Quote Originally Posted by Programmer_P View Post
    No, I'm not. I needed a container which stores two kinds of values, and allows access to one kind of value with the other kind of value, and the map (or multimap) fits that bill.
    Yes, tie your hands together behind your back while you're at it. It's like trying to give advice to a brick wall. Apparently no one on the entire forum has any sort of knowledge or perspective that's of use to you. Take a step back, try to get out from under your ego for a while and you could actually learn something, instead of feeding this insane fest of miscommunication and denial.
    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. #78
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    @Programmer_P: I'm just going to re-iterate the point I tried very hard to make earlier, that this should be a class containing a map, not just a map. Simple. Or you can keep digging this hole deeper.
    Funny. I have actually considered your suggestion seriously, though you probably think I didn't, but decided there is no advantage to putting the swapped map in a class.
    And so far, you have not really demonstrated any actual advantage to putting it in a class either that will top my method.
    Yes, tie your hands together behind your back while you're at it. It's like trying to give advice to a brick wall. Apparently no one on the entire forum has any sort of knowledge or perspective that's of use to you. Take a step back, try to get out from under your ego for a while and you could actually learn something, instead of feeding this insane fest of miscommunication and denial.
    I'm not saying that at all. Yes, I know everyone here has knowledge, more experience, perspective, etc. Otherwise, I wouldn't bother asking any question here at all. But once again, just because you have that, that doesn't make you right all the time either. At least I admit when I'm wrong...
    Last edited by Programmer_P; 06-20-2010 at 02:46 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #79
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And so far, you have not really demonstrated any actual advantage to putting it in a class either that will top my method.
    Every example has but b/c you are so blinded by your own ideas you fail to see them. Every response we get is essentially 'my way is better' and then you ask us to show you a better way. We do show you but you don't see it b/c you refuse to see it.

    At least I admit when I'm wrong...
    Well in this case you are wrong and you won't admit it. As we have all said time and time again just b/c you can create constructs in the map or with the map or what have you does not mean your design is good. Just b/c it works doesn't mean it's a good design and that you should continue with it. Bad design will code you into a corner and some of those might be impossible to get out of without significant changes. We are telling you that you are using a map for the wrong task. I'm not even sure your program requires said task but you won't listen to that either.

    But alas you will not listen and will respond to this with your typical '"I'm right and I know it so show me something different...." and yet we have and you refuse to acknowledge it.

    I'm not saying that at all. Yes, I know everyone here has knowledge, more experience, perspective, etc. Otherwise, I wouldn't bother asking any question here at all. But once again, just because you have that, that doesn't make you right all the time either.
    I have not seen anything incorrect from the others that have posted here. They are different approaches that are all fundamentally better than yours. The only posts that I find things that are plainly wrong are all yours.

  5. #80
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    Yes, I know everyone here has knowledge, more experience, perspective, etc. But once again, just because you have that, that doesn't make you right all the time either.
    In theory I agree.

    Here's what I think your problem is: you are happy about the things you have done already and don't want to change anything. However, because you have not really written very many serious programs, you are naturally short sighted WRT to design. That's how it goes. Now you are constantly trying to adapt what you have without confronting this fact, which might be hard for you to see, especially if you don't want to. It's called "stubborn beyond belief" People are just being honest and then becoming exasperated at your attitude. Pretty clearly I am not the only one. Like I said, you very much need to get a grip on your ego. It's not any of us who are going to suffer the consequences here, remember. It's up to you to make the most of it, and you really ain't. Seriously.

    Ideally, a map should allow efficient access by sequential index of the the current key value, i.e. so that you increment the index iterator to access the next key value of the map, and hence, also the mapped value. I was complaining mostly about the syntax necessary to achieve that goal in a map or multimap.
    Ah, but there's something you do not understand about why and how a map is not an array/vector, and that it is not arranged sequentially in memory. That is an inescapable trade-off of data structures (hopefully one day you'll code some yourself). This is a "can't have your cake and eat it too" scenario. The developers of C++, after all, cannot just make up anything they like because it would be nice. They are logically constrained by certain realities, this is one of them. Technically a map is a tree, if you want to look that up.
    Last edited by MK27; 06-20-2010 at 03:03 PM.
    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

  6. #81
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Bubba View Post
    Every example has but b/c you are so blinded by your own ideas you fail to see them. Every response we get is essentially 'my way is better' and then you ask us to show you a better way. We do show you but you don't see it b/c you refuse to see it.
    I do wish people would stop quoting my posts I direct at other posters. Bubba, what you just quoted was directed at MK27. It was not directed at you.
    And your statement about "every example has" is utter BS, btw. We're talking about putting a swapped map in classes here, and what advantage there would be (if any) if I put it in a class. All I'm saying is MK27 has not demonstrated a possible advantage of this at all. And I don't really believe there is one. My reasoning is based on the fact that his method would only work with one particular map (i.e. the enumerationsMap swapped around) and so I don't see that that truly justifies creating a said swapped map inside the class for that, because the swap() function provides a way for swapping ANY map around. And the only reason why I was wanting to swap the enumerations map around to begin with was for the rare times when someone might need the enumerations map to look like this:

    multimap <unsigned int, string> aMap;

    instead of

    map <string, unsigned int> aMap;

    When I decided to write such a function to swap the map around to begin with, I was thinking along the lines of maybe sometime needing to retrieve the string names of the enumerations by passing in the corresponding unsigned int value of the enumerations. Since I was thinking that, though I did recognise from the start that I could put another map in the enum class with the types the opposite of what they are in the main map, then write a function for retrieving the other map too, I decided to write a swap() function which could swap any map around to make it more flexible, seeing as I already have other ways of getting the string names of the enumerations. That is how this thread was born to begin with.
    Now I have such a working function, and MK27 seems to be supporting the idea of storing both maps in a class, and I am questioning WHY because personally I don't see any advantage over just using swap(). That is all. So far he has not really given a good explanation/reason to do such a thing, though I'm eager to read it.
    Well in this case you are wrong and you won't admit it. As we have all said time and time again just b/c you can create constructs in the map or with the map or what have you does not mean your design is good. Just b/c it works doesn't mean it's a good design and that you should continue with it. Bad design will code you into a corner and some of those might be impossible to get out of without significant changes. We are telling you that you are using a map for the wrong task. I'm not even sure your program requires said task but you won't listen to that either.
    I think you should leave that judgment to the person who is actually coding the program, and is the one reading all the code (namely, me). In this particular thread, I'm not asking about the advice of anyone regarding why I need such a program to begin with, because we've already been through all of that already, and I think it is dumb on your part to jump to such a conclusion without knowing all the facts (i.e. by reading the code of my program).
    But alas you will not listen and will respond to this with your typical '"I'm right and I know it so show me something different...." and yet we have and you refuse to acknowledge it.
    That is not what I'm saying at all. I never once said this in this thread, I don't think, that I'm right and everyone else is wrong, like it seems that's what you're suggesting here. I'm only making the case that everyone else shouldn't just automatically assume I'm wrong either just because one poster has more experience/background/etc. than I do.
    Read the posts and gather all the facts before you pass a judgment, please. Half of the posts in this thread by other posters have been filled with misguided assumptions, probably because they only skimmed the thread, and did not actually take the time to read it and think about the problems logically.
    I have not seen anything incorrect from the others that have posted here. They are different approaches that are all fundamentally better than yours. The only posts that I find things that are plainly wrong are all yours.
    Clearly, you are biased against me for some reason, because almost all posts by you directed at me in all threads I have made on these forums so far have been filled with negative comments like "Everyone else is right, and you're wrong" type of attitude.
    Spare me the personal attacks, and either start posting with more positive thoughts and comments, or stop posting in my thread completely.
    You didn't start this thread, I did. So I say if you don't like some of what I'm saying in my own thread, then you just stay the hell away from the thread and don't read it at all, how about it.
    Last edited by Programmer_P; 06-20-2010 at 03:32 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #82
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Wow. You are unbelievable.

    Spare me the personal attacks, and either start posting with more positive thoughts and comments, or stop posting in my thread completely.
    This is an open forum on the internet; if you can't handle it, leave.

    Soma

  8. #83
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    O_o

    Wow. You are unbelievable.



    This is an open forum on the internet; if you can't handle it, leave.

    Soma
    You don't think I know that? Expressing my feelings on the subject is totally different than not being able to "handle it", as you put it.
    I can only make a suggestion is all. Obviously, its up to the poster to decide to follow it or not.

    How would you like it if you started a thread asking for some honest help, then everyone just starts attacking you in it?
    Last edited by Programmer_P; 06-20-2010 at 03:44 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #84
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by MK27 View Post
    In theory I agree.

    Here's what I think your problem is: you are happy about the things you have done already and don't want to change anything.
    Correction: I don't want to change anything if I don't have to or see a reason to.
    However, because you have not really written very many serious programs, you are naturally short sighted WRT to design. That's how it goes. Now you are constantly trying to adapt what you have without confronting this fact, which might be hard for you to see, especially if you don't want to. It's called "stubborn beyond belief"
    That may be, but I have considered in this particular case at least, the long-term uses for an enumerations map and a swapped map. And like already mentioned, I just don't see why you would want to put the maps inside a class. If you're thinking in terms of objects with the maps contained in this or that object, then it would be easy enough to add a method for adding a map (swapped or otherwise) to a struct or class after the fact.
    People are just being honest and then becoming exasperated at your attitude. Pretty clearly I am not the only one. Like I said, you very much need to get a grip on your ego. It's not any of us who are going to suffer the consequences here, remember. It's up to you to make the most of it, and you really ain't. Seriously.
    I think its much more than that. It is apparent that people are skimming through this thread, then responding to certain things I say which they want to oppose, instead of providing helpful tips. At least, that's the way it is now...
    At the beginning, there were some pretty good helpful posts (laserlight's for example).
    But now the thread has deteriorated into a verbal war with words.
    Ah, but there's something you do not understand about why and how a map is not an array/vector, and that it is not arranged sequentially in memory.
    And that is fine. The way I see it, it doesn't need to be arranged sequentially in memory, to have such functionality in the std::map or std::multimap classes. The elements could be arranged in the map in the same order that they were inserted, and you could retrieve them in the same manner.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  10. #85
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    This is almost certainly going to be a waste of time, but I'm forced to wait eleven more minutes until I can eat my dinner anyway.

    How would you like it if you started a thread asking for some honest help, then everyone just starts attacking you in it?
    If that was all they were doing, you would almost have a reason to complain. Posts "attacking" you are not all that you are getting. You might have had an argument for complaining, but you do nothing but antagonize the people trying to help you.

    Do you have any idea how many newbies would love to get the attention you've gotten? You've gotten many times the average level of attention from some of the best coders this forum has to offer. Instead of listening to your superiors, you've deluded yourself into thinking that you know better because people "aren't being fair", "don't understand your code", "don't think outside the box", or "refuse to try new things".

    When I was a newbie, I didn't get a lot of good help. I was "too young" for the few local groups to give me enough credit to bother teaching me. I'd have loved to get the attention you've gotten.

    You complain about a few harsh words? For the advice you've been given? For all the shoulders trying to lift you up beyond "useless newbie"?

    Soma

  11. #86
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    This is almost certainly going to be a waste of time, but I'm forced to wait eleven more minutes until I can eat my dinner anyway.
    I LOL'ed when I read that.
    If that was all they were doing, you would almost have a reason to complain. Posts "attacking" you are not all that you are getting. You might have had an argument for complaining, but you do nothing but antagonize the people trying to help you.
    That is true, and I have already mentioned that I appreciated the helpful posts in this thread. I was referring mostly to where the thread is currently at.
    Do you have any idea how many newbies would love to get the attention you've gotten? You've gotten many times the average level of attention from some of the best coders this forum has to offer.
    Yes, I've noticed that. It seems that people are far more interested in what I say then they would like me to believe. At the very least, they must enjoy arguing with me.
    Instead of listening to your superiors, you've deluded yourself into thinking that you know better because people "aren't being fair", "don't understand your code", "don't think outside the box", or "refuse to try new things".
    No, its not about any of those things at all in this case.
    It is more that I have been working several hours on the whole swap the map idea, and so I believe I have a better idea of what it is I actually want to do then some of the posters in this thread.
    When I was a newbie, I didn't get a lot of good help. I was "too young" for the few local groups to give me enough credit to bother teaching me. I'd have loved to get the attention you've gotten.
    I'm sorry about that. Maybe you should have provoked some people too...?
    I've always had this theory that doing this often brings out the best in people, because it makes them think harder in order to prove a point or to win an argument, and so they end up going to more effort and time just to prove themselves right then they would normally on a problem, thus often bringing everyone to a much clearer understanding on the topic at hand.
    You complain about a few harsh words? For the advice you've been given? For all the shoulders trying to lift you up beyond "useless newbie"?

    Soma
    Nah, I'm not really complaining.
    Think of it as defending myself.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  12. #87
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I LOL'ed when I read that.
    O_o

    I'm actually freaking sick so I need to eat and take medications at fairly specific intervals.

    Soma

  13. #88
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by phantomotap View Post
    O_o

    I'm actually freaking sick so I need to eat and take medications at fairly specific intervals.

    Soma
    Oh...
    I thought you just meant you were waiting until dinner was ready.
    Sorry about that. Don't mean to offend you.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  14. #89
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Programmer_P View Post
    Yes, I've noticed that. It seems that people are far more interested in what I say then they would like me to believe. At the very least, they must enjoy arguing with me.
    I think most of us enjoy programming and appreciate enthusiasm, otherwise we would not be doing this at all, and you are enthusiastic.

    I am not out to attack or belittle you.

    BUT: you might want to consider the fact that you have now spent several days and scores of posts dealing with what really, truly, honestly, should be fairly simple, mostly because every time someone provides you with constructive criticism or advice, you take this as a personal affront and a challenge to your (nearly non-existent) skills and are determined to make a show out of it. That's not necessarily unusual, I have been a little bit like that before too -- just not on the same scale.

    You are not, it seems, a very fast learner, and that is okay, but being a slow learner and completely delusional is not a good combination. I'm just saying this in the hope that your slowness is not a result of low intelligence, but because of a personality trait that you could try and work on. You are stubborn and miss much forest for the trees, because you are more concerned about arguing instead of trying to understand what is being explained to you. If you can't change, you will just continue to get into excessive, mostly pointless threads like this, instead of learning C++ programming in a more serious way.

    Again, I really am not trying to insult you. If I were some kind of school authority, I would take you aside and explain this to you for your own good, and that I wish I could make you understand. You need to trust people more. Even if they seem a little rude or arrogant to you.

    Let me make an observation: after several years and 6500+ posts here, I can honestly say I can remember only ONE person with a worse attitude in this sense (repeatedly got into endless threads and seemed to learn nothing from them because s/he was convinced everything everyone said was wrong). That person got worse and worse and was eventually banned, and I'm sure no one missed them either. I don't think you are quite as crazy, Programmer_P, and I hope that's not your future. Take this to heart. Please.

    Speak less. Listen more. Try to come up with more concise examples when you have a problem instead of asking everyone to follow along with your big project, which is probably a bad habit to have gotten into. Most of the other people here do that, and I am sure they get much more out of the experience, WRT to coding.
    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

  15. #90
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Thumbs up

    Quote Originally Posted by MK27 View Post
    I think most of us enjoy programming and appreciate enthusiasm, otherwise we would not be doing this at all, and you are enthusiastic.

    I am not out to attack or belittle you.
    That is good to know.
    BUT: you might want to consider the fact that you have now spent several days and scores of posts dealing with what really, truly, honestly, should be fairly simple, mostly because every time someone provides you with constructive criticism or advice, you take this as a personal affront and a challenge to your (nearly non-existent) skills and are determined to make a show out of it. That's not necessarily unusual, I have been a little bit like that before too -- just not on the same scale.
    Technically, it wasn't several days working on this problem itself. Most of the time I was just browsing this forum, reading what was posted in my thread, and responding to it. And I think it was only a couple of days. The actual swap problem itself didn't take too long. I just had to work out the syntax to use with a templated function expecting a map and returning a multimap is all.
    You are not, it seems, a very fast learner, and that is okay, but being a slow learner and completely delusional is not a good combination.
    That is true. I tend to get off to a slow start with most things in my life, and then a while later, start to kick butt on whatever it is. So be careful what you say to me now...
    You might one day want my advice on a programming problem, instead of the other way around, no matter how doubtful that seems (even to me). haha.
    I'm just saying this in the hope that your slowness is not a result of low intelligence, but because of a personality trait that you could try and work on.
    Yeah, I would say it is a personality trait. Sometimes I find communicating with people in debates/arguments, etc. to be far more fun than actually doing whatever it is we're discussing. So I usually take my time with the subject matter, and prefer to learn it over the process of time dealing with real people such as yourselves on these forums.
    You are stubborn and miss much forest for the trees, because you are more concerned about arguing instead of trying to understand what is being explained to you. If you can't change, you will just continue to get into excessive, mostly pointless threads like this, instead of learning C++ programming in a more serious way.
    I think that is a good point there. Admittedly, I do like to argue, and it helps me think about a particular problem I'm trying to solve better, strangely enough. I do apologize if this sometimes gets out of hand.
    Again, I really am not trying to insult you. If I were some kind of school authority, I would take you aside and explain this to you for your own good, and that I wish I could make you understand. You need to trust people more. Even if they seem a little rude or arrogant to you.
    Trust...
    Yeah, trust is something that I'm not real big on, mainly because trusting someone requires you to make an assumption that the person you're trusting will live up to whatever it is they said they were going to do, etc. And in my life, most of the time when people made promises to me, they didn't keep them (including a girl I was in love with).
    Let me make an observation: after several years and 6500+ posts here, I can honestly say I can remember only ONE person with a worse attitude in this sense (repeatedly got into endless threads and seemed to learn nothing from them because s/he was convinced everything everyone said was wrong).
    Dont get me wrong here, please. I have actually learned stuff from every thread I created on here, thanks to the posts created by y'all. True, sometimes I am slow in coming around to your viewpoint, but usually I eventually do end up doing whatever it is that was advised, if it seems advantageous for my program.
    That person got worse and worse and was eventually banned, and I'm sure no one missed them either. I don't think you are quite as crazy, Programmer_P, and I hope that's not your future. Take this to heart. Please.

    Speak less. Listen more. Try to come up with more concise examples when you have a problem instead of asking everyone to follow along with your big project, which is probably a bad habit to have gotten into. Most of the other people here do that, and I am sure they get much more out of the experience, WRT to coding.
    Now, that's some good advice I can recognize as such.
    Hope to see more of it in the future.

    Anyway, I guess this thread can be closed now. The original problem has already been solved, and now we've been going off-topic (a big thing, I know, on some other forums, don't know about this one) for quite a while now.
    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