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;

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.

Lerner
std::swap looks good for me. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.