Thread: Swap values inside of a vector ?

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

    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,336
    What's wrong with std::swap from <algorithm>?

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    67
    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
    28,413
    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]);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,336
    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
    67
    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, 09: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, 04:22 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM