0

Everybody hi.
I need to swap elements of my std::vector container.
For example, i have vector with values from 1 to 5;
and i need to swap 2 with 3 (only nearby elements), that in result i'll have 1,3,2,4,5.
In real world i don't have std::vector filled with numbers - it filled with struct objects;

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by crioto
0

Do a routine swap manually or use the swap() method from STL algorithm header file. The routine swap involves identifying the two items you want to swap, declaring a third item of desired type and doing this:

temp = B;
B = A;
A = temp;

Be sure your struct types have a robust assignment operator to be able to do this type of stuff safely.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.