Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

An STL containiner with the ability to swap elements ...

This topic is 4078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anybody name an STL container that holds an ordered list of elements and has the ability to easily swap the positions of any two elements?

Share this post


Link to post
Share on other sites
Advertisement
That doesn't even make any sense. If they're ordered, swapping would put them out of order.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
That doesn't even make any sense. If they're ordered, swapping would put them out of order.


Sorry what I meant by ordered was that is isn't an associative container but a sequence container.

For example, lets say I have a vector: how can I swap the elements at position 2 and 3 .

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane

std::vector<int> vec;
// fill the vector
std::swap(vec[2], vec[3]);


Hmm, I originally thought swap would be a member function of the container.

I am curious how that works. vec[] should return an element of the type you defined in the template (int). So if you pass swap two ints by value, how does it re-arrange them in the original vector?

Share this post


Link to post
Share on other sites
The hard way. It'll generate code like:

int temp = vec[2];
vec[2] = vec[3];
vec[3] = temp;

Oh, and operator[] actually returns a reference, not a value.

Share this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Quote:
Original post by SiCrane
That doesn't even make any sense. If they're ordered, swapping would put them out of order.


Sorry what I meant by ordered was that is isn't an associative container but a sequence container.

Indeed, SiCrane was describing a sorted container.

Quote:
For example, lets say I have a vector: how can I swap the elements at position 2 and 3 .


std::vector< int >::iterator first = vec.begin(), last = first;
std::advance( first, 2 );
std::advance( last, 3 );

std::swap( *first, *last );


Or, if you have Boost:

std::swap( *boost::next( container.begin(), 2 ), *boost::next( container.begin(), 3 ) );

Where boost::next():
template <class T, class Distance>
T next(T x, Distance n)
{
std::advance(x, n);
return x;
}


These will work for any ordered container with forward iterators or "better." (Where the iterators point to the actual elements)


jfl.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
The hard way. It'll generate code like:

int temp = vec[2];
vec[2] = vec[3];
vec[3] = temp;

Oh, and operator[] actually returns a reference, not a value.


What do you mean, 'the hard way'?

Share this post


Link to post
Share on other sites
The post I responded to has been edited at least twice, so the line no longer makes sense.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!