POV-Ray : Newsgroups : povray.off-topic : Swapping vector elements Server Time
31 Oct 2024 12:15:32 EDT (-0400)
  Swapping vector elements (Message 1 to 4 of 4)  
From: Orchid Win7 v1
Subject: Swapping vector elements
Date: 14 Jan 2014 16:21:30
Message: <52d5aa5a$1@news.povray.org>
OK, so we have a std::vector of pointers to task objects. How do we swap 
a pair of elements to change their order?

Half the Internet claims this is std::swap(), the other half claim it's 
std::swap_iter(), and a few people claim it's something else again...


Post a reply to this message

From: clipka
Subject: Re: Swapping vector elements
Date: 14 Jan 2014 21:06:39
Message: <52d5ed2f$1@news.povray.org>
Am 14.01.2014 22:21, schrieb Orchid Win7 v1:
> OK, so we have a std::vector of pointers to task objects. How do we swap
> a pair of elements to change their order?
>
> Half the Internet claims this is std::swap(), the other half claim it's
> std::swap_iter(), and a few people claim it's something else again...

 From the names of those function, I'd suspect both to be right: 
std::swap_iter() probably works on arbitrary containers but requires you 
to obtain an iterator first, while std::swap() might just need numeric 
indices but be limited to std::vector.


Post a reply to this message

From: Warp
Subject: Re: Swapping vector elements
Date: 15 Jan 2014 14:16:33
Message: <52d6de91@news.povray.org>
Orchid Win7 v1 <voi### [at] devnull> wrote:
> OK, so we have a std::vector of pointers to task objects. How do we swap 
> a pair of elements to change their order?

> Half the Internet claims this is std::swap(), the other half claim it's 
> std::swap_iter(), and a few people claim it's something else again...

Well, it depends on whether you are dereferencing the vector elements
directly or whether you just have iterators to them.

If you have iterators pointing to the elements, then

  std::iter_swap(iterator1, iterator2);

is just the same as

  std::swap(*iterator1, *iterator2);

If you are indexing the vector directly, then just do a

  std::swap(v[index1], v[index2]);

-- 
                                                          - Warp


Post a reply to this message

From: Orchid Win7 v1
Subject: Re: Swapping vector elements
Date: 15 Jan 2014 15:49:43
Message: <52d6f467$1@news.povray.org>
On 15/01/2014 07:16 PM, Warp wrote:
> If you are indexing the vector directly, then just do a
>
>    std::swap(v[index1], v[index2]);

This is probably pretty much exactly what the code needs to do. (The 
code sits behind a GUI where the user can reorder the contents of a list.)


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.