Swap values inside of a vector ?

This is a discussion on Swap values inside of a vector ? within the C++ Programming forums, part of the General Programming Boards category; Moin, I can't find a funktion to swap 2 contents inside of a vector. Isn't there an easy way to ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    66

    Swap values inside of a vector ?

    Moin,

    I can't find a funktion to swap 2 contents inside of a vector.
    Isn't there an easy way to do that ?


    Greetz
    Greenhorn

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with std::swap from <algorithm>?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    The description says that it swaps from two different vectors, but not inside of one container ...
    Does it although work ???

    EDIT:
    Ah, sorry, I'll try the std::swap function, but the exapmles are also using two different containers ...

    Greetz
    Last edited by Greenhorn__; 09-23-2008 at 01:37 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    The description says that it swaps from two different vectors, but not inside of one container ...
    You might be looking at vector's own swap, which swaps two vectors. The generic swap algorithm, on the other hand, swaps any two objects of the same type (assuming that they can be swapped).

    For example, given a vector named vec with two elements, we could write:
    Code:
    std::swap(vec[0], vec[1]);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It swaps two objects, whatever you want those objects to be. They can be integers, vectors, or whatever. I don't know where you're reading the description from, but it will swap anything.

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    66
    Thanks, you both !!! I tried it and it works fine with std::swap.

    @laserlight
    That's exactly what I wanna do

    Now I can sleep well, tonight ...

    Once again: Thank you laserlight and tabstop !


    Greetz

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector of Strings help
    By CowsOFFmylawn in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2009, 08:38 PM
  2. ordering vector elements based on heuristic values
    By cyberfish in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2007, 12:33 AM
  3. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  4. how do i read 2 consecutive values in vector and compute?
    By dalearyous in forum C++ Programming
    Replies: 8
    Last Post: 03-30-2006, 03:22 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 06:33 PM

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