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, 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
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.
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)
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.
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)
<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
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.
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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
{
|
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;
|
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:
| |
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.
|
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.
|
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
|
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;
|
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
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |