"Howard" <me@here.comwrote in message
news:FL******************************@comcast.com. ..
Hi all,
is there an easy way to swap one pointer item in a vector with a
pointer that's not yet in the vector?
Currently, I'm using begin()+index to get an iterator to the item I
want to swap out, then deleting the pointer there, then using erase to get
rid of the vector item itself and get an iterator to the next item, and
finally using insert to put my new pointer in the correct place. Is there
an easier way?
Just change it. The output of the following program is
10 20
10 10
#include <iostream>
#include <vector>
int main()
{
std::vector<int*MyVect;
int a = 10;
int b = 20;
MyVect.push_back( &a );
MyVect.push_back( &b );
for ( std::vector<int*>::iterator it = MyVect.begin(); it !=
MyVect.end(); ++it )
std::cout << *(*it) << " ";
std::cout << "\n";
std::vector<int*>::iterator it = MyVect.begin() + 1;
*it = &a;
for ( std::vector<int*>::iterator it = MyVect.begin(); it !=
MyVect.end(); ++it )
std::cout << *(*it) << " ";
std::cout << "\n";
}