473,830 Members | 2,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vector Iterator

Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass* myvector;
....
vector<MyClass* >::iterator it;

for(it = myvector.begin( ); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember( );
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Thanks alot for any help,
Jun 27 '08 #1
8 1781
On 2008-04-22 19:25, JackC wrote:
Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass* myvector;
...
vector<MyClass* >::iterator it;

for(it = myvector.begin( ); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember( );
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?
Use either

(*it)->myclassmember( );

or

(**it).myclassm ember();

--
Erik Wikström
Jun 27 '08 #2
On 22 Apr, 18:50, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2008-04-22 19:25, JackC wrote:
Hi,
If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?
for example,
vector<MyClass* myvector;
...
vector<MyClass* >::iterator it;
for(it = myvector.begin( ); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember( );
}
What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Use either

(*it)->myclassmember( );

or

(**it).myclassm ember();

--
Erik Wikström
Thanks very much, works fine.
Jun 27 '08 #3
On 2008-04-22 13:50:53 -0400, Erik Wikström <Er***********@ telia.comsaid:
On 2008-04-22 19:25, JackC wrote:
>Hi,

If i have a std::vector containing pointers, then iterate through the
vector and im slightly confused as to how i use the item from the
iterator?

for example,

vector<MyClass *myvector;
...
vector<MyClass *>::iterator it;

for(it = myvector.begin( ); it < myvector.end(); it++)
{
// This doesn't work,
**it->myclassmember( );
}

What am i doing wrong? I thought the iterator is a pointer to a
pointer in this case?

Use either

(*it)->myclassmember( );

or

(**it).myclassm ember();
Or

it->myclassmember( );

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #4
On Apr 22, 8:36 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia. comsaid:
...
Or

it->myclassmember( );
...

Is that possible? myvector is a vector of pointers.

Regards.
Jun 27 '08 #5
On 2008-04-22 16:38:22 -0400, al**********@gm ail.com said:
On Apr 22, 8:36 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia. comsa
id:
...
>Or

it->myclassmember( );
...

Is that possible? myvector is a vector of pointers.
Try it.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #6
<al**********@g mail.comwrote in message
On Apr 22, 8:36 pm, Pete Becker <p...@versatile coding.comwrote :
>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia. com>
said:
it->myclassmember( );
Is that possible? myvector is a vector of pointers.
No. Probably Pete was in a hurry.

--
http://techytalk.googlepages.com
Jun 27 '08 #7
Pete Becker wrote:
On 2008-04-22 16:38:22 -0400, al**********@gm ail.com said:
>On Apr 22, 8:36 pm, Pete Becker <p...@versatile coding.comwrote :
>>On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia. comsa
id:
...
>>Or

it->myclassmember( );
...

Is that possible? myvector is a vector of pointers.

Try it.
Ant you'll get a compile error!

--
Ian Collins.
Jun 27 '08 #8
On 2008-04-22 17:14:59 -0400, Ian Collins <ia******@hotma il.comsaid:
Pete Becker wrote:
>On 2008-04-22 16:38:22 -0400, al**********@gm ail.com said:
>>On Apr 22, 8:36 pm, Pete Becker <p...@versatile coding.comwrote :
On 2008-04-22 13:50:53 -0400, Erik Wikström <Erik-wikst...@telia. comsa
id:
...
Or

it->myclassmember( );

...

Is that possible? myvector is a vector of pointers.

Try it.
Ant you'll get a compile error!
Yup. There's an extra level of indirection that I overlooked.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #9

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

Similar topics

14
5031
by: Jim West | last post by:
I'm curious why I might be getting such a large performance difference between using a map iterator and a vector iterator. This is a computational electromagnetics code where I have to separate points in space that arrive randomly into groupings based on (x, y, z) dimensions, so I use std::map to do that. Once the sorting is done I need to find the interactions between groups, so I've been using iterators to step through the map. As a...
18
2887
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
29
3988
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My case: class A {
11
2753
by: koperenkogel | last post by:
Dear cpp-ians, I am working with a vector of structures. vector <meta_segment> meta_segm (2421500); and the structure look like: struct meta_segment { float id; float num;
17
3363
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
3
2929
by: codefixer | last post by:
Hello, I am trying to understand if ITERATORS are tied to CONTAINERS. I know the difference between 5 different or 6(Trivial, on SGI). But what I fail to understand is how can I declare all 5 kinds of iterators on say a vector. OR is it that any iterator declared on Vector is Random Iterator which has the functionality of all the others.
9
5219
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10); vector<double>::iterator i=x.begin()+2, j=x.begin()+6; x.erase(i,j); // i<j, ok, erases 4 elements. x.erase(j,i); // j>i, no error, just inserts 4 elements.
4
5710
by: arnuld | last post by:
i wrote a programme to create a vector of 5 elements (0 to 4), here is the code & output: #include <iostream> #include <vector> int main() { std::vector<intivec; // dynamically create a vector
1
6231
by: atomik.fungus | last post by:
Hi, I'm re-writting my matrix class to practice my programming and the computer doesn't let me compile the next code: ( this example come from the constructor of the class) //the matrix is made of vectors of vectors template< typename T > matrix< T >::matrix( unsigned rows, unsigned columns ) { vector< vector< T ::iterator p;
12
4920
by: desktop | last post by:
Why does insert only work when specifying an iterator plus the object to be inserted: std::vector<intt; std::vector<int>::iterator it; it = t.begin(); t.insert(it,33); If I use push_back instead I don't need to supply the iterator. But what
0
9791
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10771
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10487
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9313
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7745
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3958
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.