swap()

This is a discussion on swap() within the C++ Programming forums, part of the General Programming Boards category; I have a map that's indexed with integers as the keys. I can't find anything on this function except for ...

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    swap()

    I have a map that's indexed with integers as the keys. I can't find anything on this function except for an example that doesn't even use maps. Can anybody help me out?

    All I have so far is

    swap(3);

    But I get the following error: no matching function for call to`swap(int)'

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    http://www.cppreference.com/cppmap/swap.html

    EDIT: and save the domain in you bookmarks
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    It doesn't work, i get the error:

    invalid suffix "swap" on floating constant

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Post your code!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by divineleft
    It doesn't work, i get the error:

    invalid suffix "swap" on floating constant
    just think about it:
    it's called 'swap' so in any way two parties have to be involved to swap.
    it shurely is possible in some context to call swap with only one argument ( e.g. call an iterator's swap member with another iterator as argument ) but you shurely cannot call swap with a literal.
    You have to provide some context to get a proper answer.
    Kurt

  6. #6
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Code:
        inv_iter = inventory.find(number);
        for(inv_iter; inv_iter!=inventory.end(); inv_iter++)
        {
                  2.swap(3);
        }
    the 2 and 3 are just temporary

  7. #7
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    2 and 3 are illegal identifiers (variable names) in C/C++ - if you
    don't know that then you probably shouldn't be asking about
    swap...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  8. #8
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    oh

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    What is it you want to do?
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Well for my game, the users inventory uses maps indexed by numbers (0,1,2,3) which are all assigned to the item when they are added to the inventory. So when the user inputs the number, it is associated with an item and that item is removed. I want it so the map that the user inputted is swapped with the next number in the map until it reaches the end of the map. Once it is at the end It can be removed. This way when the item is removed the inventory doesn't display:

    1 - (item)
    3 - (item)
    4 - (item)

    It should be:

    1 - (item)
    2 - (item) (swapped)
    3 - (item) (swapped)
    4 - (item) (deleted)

  11. #11
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Never mind, I just had to use " swap(inventory[2], inventory[3]);"

    delete/lock/ban/prob me

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Wouldn't it be easier to use a plain old vector, and display the contents with the index number. If the user selects a number, just erase the element at that index.

  13. #13
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    I thought of using that at first but I was told to use maps in my other thread. I guess it kind of makes sense since the 1,2,3,4's are always with the data.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,404
    The idea to use the map on another thread was if you wanted to associate each item to a specific key. For instance, all Potions of Healing having key 238, Potions of Super Healing having key 436, Longswords of Maiming Multiple and Hideous Times having key 112.

    That way you could access any of these elements directly by using a map or multimap for your inventory. You can access and erase elements without the need to create For loops. You simply provide the key to specific functions like erase(), count() and find().

    However, if you only want to have items ordered then a vector is a better solution.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list swap function
    By Axel in forum C Programming
    Replies: 32
    Last Post: 01-16-2011, 08:40 AM
  2. Atomic compare and swap.
    By Maz in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 02:47 PM
  3. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 05:32 AM
  4. Double Link List Swap function
    By kennny2004 in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2006, 11:52 AM
  5. Skipping DX swap chain node - not good...
    By Magos in forum Game Programming
    Replies: 0
    Last Post: 03-02-2006, 07:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21