Skip to main content
gamedev.net
🔒 Locked

[solved] Moving elements within an std::vector

Started by Kustaz 2 replies 900+ views
Original Post
Kustaz
Kustaz
Is there a built in method for relocating an element from one position within a vector to another? I googled for a solution but didn't find anything which was very helpful. [Edited by - Kustaz on September 16, 2005 1:00:26 AM]
spock
spock
In general you just copy the element to its new position. If you are moving a range of elements you're probably looking for std::copy, for a single element regular assignment is fine.
Kustaz
Kustaz
Oh ok thanks, i just wasnt sure if there was a ore 'cut' like way of doing things.
MaulingMonkey
MaulingMonkey
Quote:
Original post by Kustaz
Is there a built in method for relocating an element from one position within a vector to another? I googled for a solution but didn't find anything which was very helpful.


Vectors don't have any built in operations for this (AFAIK), I'd use something like:

std::swap( my_vector[3] , my_vector[15] );

Or, with iterators:

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

Etc.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.