Sign in to follow this  
rish2005

[C++ STL]swapping elments in a vector??

Recommended Posts

I am using C++ STL vector implentation. My vector is holding pointers to the object. class objclass { private: int data; public: objclass(int i); . . . ~objclass(); }; vector<objclass *> objvector; I want to swap to elements of the vector... say objvector[i] & objvector[j].. which is the most efficient way to do the same.

Share this post


Link to post
Share on other sites
Quote:
Original post by rish2005
I want to swap to elements of the vector... say objvector[i] & objvector[j]..
which is the most efficient way to do the same.


Smells like premature optimization, to swap 2 elements just do:


#include <algorithm> // std::swap

.....
std::swap(objvector[i], objvector[j]);

Share this post


Link to post
Share on other sites
thanks...
I will be using swap in a multithreaded environment where one thread will be inserting new elements in the vector. Is it thread safe..

Share this post


Link to post
Share on other sites
Quote:
Original post by rish2005
thanks...
I will be using swap in a multithreaded environment where one thread will be inserting new elements in the vector. Is it thread safe..
No..
You'll have to place some kind of lock (mutex) around the vector to be safe, or use another datastructure. a deque *should* be safe.

Share this post


Link to post
Share on other sites
Using a deque won't help. std::swap just isn't threadsafe (in fact the whole of the C++ standard is thread-agnostic). Consider the following:
Thread 1                                Thread 2
call deque.push_back(1)
write 1 to deque memory
increment size
call deque.push_back(2)
write 2 to deque memory
increment size
call std::swap(deque[0], deque[1])
temp int tmp = deque[0]
call deque.push_back(3)
write 3 to deque memory
increment size
call std::swap(deque[0], deque[2])
temp int tmp = deque[0]
deque[0] = deque[2]
deque[2] = tmp
deque[0] = deque[1]
deque[1] = tmp

now deque[0] = 2, deque[1] = 1 and deque[2] = 1, even though you inserted the numbers 1, 2 and 3 and only performed swap operations.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
now deque[0] = 2, deque[1] = 1 and deque[2] = 1, even though you inserted the numbers 1, 2 and 3 and only performed swap operations.
I didn't mean to say that it's completely safe, and that's far from the only unsafe example.
However it should (hopefully..) be safe for communicating between a single producer and a single consumer thread.

Still.. It may be a better idea to write a custom datastructure if it's threading properties must be guaranteed.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Datastructures usually shouldn't need to worry about threads. Having them lock their own operations would guarantee their own invariants, but code using the containers usually adds additional invariants so would need to lock -- with a different mutex -- anyways.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this