|
|
|
|
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |
| |
|
|
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
|
|
| |
| |
|
|
|
|
| |