473,387 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Swapping a single pointer item in vector?

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

Dec 5 '07 #1
2 1425
"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";
}
Dec 5 '07 #2

----- Original Message -----
From: "Jim Langston" <ta*******@rocketmail.com>
Newsgroups: comp.lang.c++
Sent: Tuesday, December 04, 2007 7:01 PM
Subject: Re: Swapping a single pointer item in vector?

"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. std::vector<int*>::iterator it = MyVect.begin() + 1;
*it = &a;
D'oh! Of course! :-) Thanks, Jim
-Howard

Dec 5 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Al Newton | last post by:
My class has this member function: vector<UniqueCustId*>* CMyClass::GetCustList( void ) { SYSTEMTIME st; string strServiceDate; UniqueCustId* uci = new UniqueCustId; string strCustNumberOld =...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
7
by: deancoo | last post by:
Ok, I've got a vector I've filled with a LOT of data, and after massaging it just right, I need to copy it all over to a map where I'll have the advantage of assigning a custom key as the key...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
4
by: Damien | last post by:
Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument,...
5
by: yancheng.cheok | last post by:
hello, may i know how i can determine whether a pointer is pointing to an array or a single item during runtime? this is because in certain situation, i need to determine whether to use delete...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.