Click to See Complete Forum and Search --> : Swapping Vector


bbrunno
April 2nd, 2003, 05:39 PM
I have a vector of objects, the type is not important

At some point, I have an object down the list that I would like to move farther up the list (an example follows)

What is the best way to do this?

Example:

1,2,3,4,5,6,7,8,9

...then...

1,2,3,7,4,5,6,8,9

Notice how seven is promoted to vector[3]? What would be the best way to do this?

Thanks

mwilliamson
April 2nd, 2003, 05:50 PM
did you try the swap function?

bbrunno
April 2nd, 2003, 06:13 PM
I have my list....moduleList

tried moduleList.swap(4,8)

where 4 & 8 are positions

returned an error.

Philip Nicoletti
April 2nd, 2003, 06:35 PM
I'm don't think there is a good way to do this with vectors.
Basically, you are erasing and element and inserting a new
element. These operations are inefficient for vectors, except
at the end. There are two methods I can think of do this.
One would be to do an actual erase/insert. Another would be to
use rotate(). I think that rotate() might be slightly more
efficient, but I am not sure.

[ I noticed in your last post that you mentioned a list,
instead of vector. Which is it ? ]


#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;


int main()
{
int i;

// method 1 : using erase/insert

vector<int> v1;

for (i=1; i<10; ++i) v1.push_back(i);

copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;

int tmp = v1[6];
v1.erase(v1.begin()+6);
v1.insert(v1.begin()+3,tmp);

copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;

// method2 : using rotate

vector<int> v2;

for (i=1; i<10; ++i) v2.push_back(i);

copy (v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;
rotate(v2.begin()+3,v2.begin()+6,v2.begin()+7);

copy (v2.begin(),v2.end(),ostream_iterator<int>(cout," "));
cout << endl;

return 0;
}

Philip Nicoletti
April 2nd, 2003, 07:02 PM
FYI, if you have a std::list instead, I think the splice()
member function is best:


#include <list>
#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;


int main()
{
int i;

// method : std::list::splice() member function

list<int> v1;

for (i=1; i<10; ++i) v1.push_back(i);

copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl;

list<int>::iterator i1 = v1.begin();
advance(i1,3);

list<int>::iterator i2 = v1.begin();
advance(i2,6);

v1.splice(i1,v1,i2);

copy (v1.begin(),v1.end(),ostream_iterator<int>(cout," "));
cout << endl << endl;


return 0;
}

Paul McKenzie
April 2nd, 2003, 07:08 PM
Originally posted by bbrunno
I have my list....moduleList

tried moduleList.swap(4,8)

where 4 & 8 are positions

returned an error. There is no function that is a member of std::list<T> called swap() that takes two integers.

The only swap function that is also a member of list<T> swaps two lists. Also, a majority of the STL functions do not use "position numbers", but instead iterators are used. If you are calling an STL function and you are refering to your items by "position number" instead of iterators, look up the function in your favorite STL reference and make sure that what you are doing is correct.

The std::swap function and iter_swap are the ones that you are looking for if your goal is to swap two values.

Regards,

Paul McKenzie

cup
April 4th, 2003, 01:06 AM
Try


#include <algorithm>
...
swap(moduleList[4], moduleList[8]);