Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Swapping a single pointer item in vector?

Reply
Thread Tools

Swapping a single pointer item in vector?

 
 
Howard
Guest
Posts: n/a
 
      12-05-2007
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?

Thanks,
-Howard

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-05-2007
* Howard:
>
> is there an easy way to swap one pointer item in a vector with a
> pointer that's not yet in the vector?


std::swap comes to mind.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      12-05-2007
"Howard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed). ..
> 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";
}


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      12-05-2007

----- Original Message -----
From: "Jim Langston" <(E-Mail Removed)>
Newsgroups: comp.lang.c++
Sent: Tuesday, December 04, 2007 7:01 PM
Subject: Re: Swapping a single pointer item in vector?


> "Howard" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed). ..
>> 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. std::vector<int*>::iterator it = MyVect.begin() + 1;
> *it = &a;


D'oh! Of course! Thanks, Jim
-Howard

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
understanding pointer-swapping arnuld C++ 5 08-13-2007 12:23 PM
determine pointer to point to array or single item during runtime yancheng.cheok@gmail.com C++ 5 07-21-2006 11:47 AM
How do I reverse a string with a single variable for swapping rajendra.stalekar@gmail.com C Programming 29 11-05-2005 03:42 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Change style of a single row of the item list of datagrid, based on a field value of current item... QUASAR ASP .Net Datagrid Control 6 01-17-2004 07:46 PM



Advertisments