473,607 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

In map iterator is there a difference between (*iter).second anditer->second?

I see that a lot of former in the code, and wonder if there is a
technical reason for that
Aug 19 '08 #1
15 2398

"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:81******** *************** ***********@56g 2000hsm.googleg roups.com...
>I see that a lot of former in the code, and wonder if there is a
technical reason for that
You're asking about language syntax, this is
not specific to map or iterators.

Given a pointer to a class or struct:

struct T
{
int member;
};

and a pointer to one of these structs:
T obj;
T *p(&obj);

The two expressions:

(*p).member

and

p->member

have exactly the same meaning.

The '->' form is 'shorthand' for the (*). form.

Why someone used the latter over the former I
don't know. Perhaps a 'style' issue, or possibly
that's what a code generator created.

-Mike


Aug 19 '08 #2
On Aug 19, 9:48*am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"puzzlecrac ker" <ironsel2...@gm ail.comwrote in message

news:81******** *************** ***********@56g 2000hsm.googleg roups.com...
I see that a lot of former in the code, and wonder if there is a
technical reason for that

You're asking about language syntax, this is
not specific to map or iterators.

Given a pointer to a class or struct:

struct T
{
* * int member;

};

and a pointer to one of these structs:
T obj;
T *p(&obj);

The two expressions:

(*p).member

and

p->member

have exactly the same meaning.

The '->' form is 'shorthand' for the (*). form.

Why someone used the latter over the former I
don't know. *Perhaps a 'style' issue, or possibly
that's what a code generator created.

-Mike
I believe Mayers mentioned something in regard to map or another
container the difference of two operators -- I don't recall it.

It's NOT the matter of style.
Aug 19 '08 #3
On 19 ago, 11:06, puzzlecracker <ironsel2...@gm ail.comwrote:
On Aug 19, 9:48*am, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"puzzlecrac ker" <ironsel2...@gm ail.comwrote in message
news:81******** *************** ***********@56g 2000hsm.googleg roups.com...
>I see that a lot of former in the code, and wonder if there is a
technical reason for that
You're asking about language syntax, this is
not specific to map or iterators.
Given a pointer to a class or struct:
struct T
{
* * int member;
};
and a pointer to one of these structs:
T obj;
T *p(&obj);
The two expressions:
(*p).member
and
p->member
have exactly the same meaning.
The '->' form is 'shorthand' for the (*). form.
Why someone used the latter over the former I
don't know. *Perhaps a 'style' issue, or possibly
that's what a code generator created.
-Mike

I believe Mayers mentioned something in regard to map or another
container the difference of two operators -- I don't recall it.

It's NOT the matter of style.
Hi.

There's no difference between the two of them. However, the following
note can be seen on the SGI documentation site for the STL:

"Defining operator-for iterators depends on a feature that is part
of the C++ language but that is not yet implemented by all C++
compilers. If your compiler does not yet support this feature, the
workaround is to use (*it).m instead of it->m."

This regards to some "old days". Today most compilers do provide such
a feature. Therefore, I agree with Mike that is probably a 'style'
issue.
--
Leandro T. C. Melo

Aug 19 '08 #4
"Mike Wahler" <mk******@mkwah ler.netwrote in message
news:q7******** *************** *******@earthli nk.com...
You're asking about language syntax, this is
not specific to map or iterators.
Not quite.
Given a pointer to a class or struct:

struct T
{
int member;
};

and a pointer to one of these structs:
T obj;
T *p(&obj);

The two expressions:

(*p).member

and

p->member

have exactly the same meaning.
Yes, but the original question was about iterators, not pointers.

If you are defining an iterator, you are expected to define both operator->
and operator*. However, some pre-standard libraries did not do so
consistently. To cater to that inconsistently, early authors tended to
prefer (*p).mem to p->mem, because the former is more tolerant of library
sloppiness than the latter.

This kind of tension comes up all the time when one author uses software
written by another. It's one of those messy details that divides theory
from practice.
Aug 19 '08 #5

"Andrew Koenig" <ar*@acm.orgwro te in message
news:aJ******** *************@b gtnsc05-news.ops.worldn et.att.net...
"Mike Wahler" <mk******@mkwah ler.netwrote in message
news:q7******** *************** *******@earthli nk.com...
>You're asking about language syntax, this is
not specific to map or iterators.

Not quite.
>Given a pointer to a class or struct:

struct T
{
int member;
};

and a pointer to one of these structs:
T obj;
T *p(&obj);

The two expressions:

(*p).member

and

p->member

have exactly the same meaning.

Yes, but the original question was about iterators, not pointers.
Oops, you're right. Although iterators are supposed
to behave like pointers, they're not pointers.
>
If you are defining an iterator, you are expected to define both
operator-and operator*. However, some pre-standard libraries did not do
so consistently. To cater to that inconsistently, early authors tended to
prefer (*p).mem to p->mem, because the former is more tolerant of library
sloppiness than the latter.

This kind of tension comes up all the time when one author uses software
written by another. It's one of those messy details that divides theory
from practice.
Thanks for the correction and explanation.

And thanks for writing that great book (Accelerated C++).

-Mike
Aug 19 '08 #6
puzzlecracker <ir*********@gm ail.comwrote:
I see that a lot of former in the code, and wonder if there is a
technical reason for that
There was this woman who always cut the end off of a ham before cooking
it.

"Why do you do that?" I asked.

"Because my mother always did."

When we asked her mother she said, "well that's the way my mother always
did it."

And when we asked *her* mother she said, "because the oven was too
small. I had to cut the end off of the ham so it would fit."

What you have discovered is much the same. Programmers don't have to use
(*iter).second anymore because the "oven is big enough now" but there is
ingrained habit that gets passed down from one to another without
complete understanding.

:-)
Aug 19 '08 #7
On Aug 19, 11:33*am, "Andrew Koenig" <a...@acm.orgwr ote:
"Mike Wahler" <mkwah...@mkwah ler.netwrote in message

news:q7******** *************** *******@earthli nk.com...
You're asking about language syntax, this is
not specific to map or iterators.

Not quite.
Given a pointer to a class or struct:
struct T
{
* *int member;
};
and a pointer to one of these structs:
T obj;
T *p(&obj);
The two expressions:
(*p).member
and
p->member
have exactly the same meaning.

Yes, but the original question was about iterators, not pointers.

If you are defining an iterator, you are expected to define both operator->
and operator*. *However, some pre-standard libraries did not do so
consistently. *To cater to that inconsistently, early authors tended to
prefer (*p).mem to p->mem, because the former is more tolerant of library
sloppiness than the latter.

This kind of tension comes up all the time when one author uses software
written by another. *It's one of those messy details that divides theory
from practice.
Sort of what Mayers said in regard to iterators and recommended on
using the older syntax.
Aug 19 '08 #8
Re: In map iterator is there a difference between (*iter).second and
iter->second?
"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:81******** *************** ***********@56g 2000hsm.googleg roups.com...
>I see that a lot of former in the code, and wonder if there is a
technical reason for that
Most people suggest the second form, iter->second, which I initially used.
But then I ran across this from some class I was developing:

#ifndef OBSERVABLE_H
#define OBSERVABLE_H

#include "Observer.h "
#include <set>

class Observable {
std::set<Observ er*observers;
public:
virtual void addObserver(Obs erver& o) {
observers.inser t(&o);
}
virtual void deleteObserver( Observer& o) {
observers.erase (&o);
}
virtual void deleteObservers () {
observers.clear ();
}
virtual size_t countObservers( ) {
return observers.size( );
}
virtual void notifyObservers (Argument* arg = NULL)
{
std::set<Observ er*>::iterator it;
for(it = observers.begin (); it != observers.end() ; it++)
(*it)->update(this, arg);
}
};

#endif

Look at the function virtual void notifyObservers (Argument* arg = NULL)
specifically at the line:
(*it)->update(this, arg);

When I first tried to code that I was trying to do:
it->->update(this, arg);
which, obviously, wouldn't compile. So I wound up with that format,
derefernce the iterator, then use the arrow operator. And I thought about
it, and dereferenceing the iterator would always work, where using the arrow
operator would usually work, but not always. So I determined that all code
working with iterators would be dereferecned instead of using the arrow
operator for consistancy. So a map would be
(*it).second
etc..

I know seeing (*it) in my code that I am dereferncing an iterator. In fact,
I reserve the variable it for a local iterator.

Other than that, there is no real difference, it comes down to personal
prefernce and whatever your workplace standards are. I figured on (*it)
because I could be consistant with it. Although it is said that consistancy
is the hobgoblin of little minds I still like being consistant if I can. If
that means I have a little mind, so be it.

Regards,

Little Minded Jim Langston

Aug 20 '08 #9
"Mike Wahler" <mk******@mkwah ler.netwrites:
"puzzlecrac ker" <ir*********@gm ail.comwrote in message
news:81******** *************** ***********@56g 2000hsm.googleg roups.com...
>>I see that a lot of former in the code, and wonder if there is a
technical reason for that

You're asking about language syntax, this is
not specific to map or iterators.

Given a pointer to a class or struct:

struct T
{
int member;
};

and a pointer to one of these structs:
T obj;
T *p(&obj);

The two expressions:

(*p).member

and

p->member

have exactly the same meaning.

The '->' form is 'shorthand' for the (*). form.

Yes, but given a different class, they may be different:

#include <iostream>

class A {
public: int x;
};

class P {
public:
A a1;
A a2;

A& operator*(){ return(a1); }
A* operator->(){ return(&a2); }
};
using namespace std;

int main(void){
P p;
p.a1.x=1;
p.a2.x=2;
cout<<"p->x = "<<p->x<<" ; (*p).x = "<<(*p).x<<endl ;
return(0);
}

/*
-*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
Compilation started at Wed Aug 20 11:40:06

SRC="/home/pjb/src/tests-c++/memb.c++" ; EXE="memb" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
p->x = 2 ; (*p).x = 1
status = 0

Compilation finished at Wed Aug 20 11:40:07
*/

Why someone used the latter over the former I
don't know. Perhaps a 'style' issue, or possibly
that's what a code generator created.
Or perhaps they mean different things.
--
__Pascal Bourguignon__
Aug 20 '08 #10

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

Similar topics

38
3658
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
4
1602
by: Arturo Cuebas | last post by:
I've got a bunch of file_iterator<> (http://tinyurl.com/3uuxa) begin/end pairs that point to various chunks in a file. It would be super-cool, in my program, to be able to treat all of these ranges as though they were one huge range. I'm envisioning something along the lines of a library facility that provides an iterator type that encapsulates a set of ranges and abstracts out code to handle traversal through all of them as though all the...
3
2274
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through the map, which means I must iterate through all of the sets in the map. I need an iterator that points to the set where the integer was found. How can I do this? What will the type of this iterator be? Can you help me with a code snippet?
21
5694
by: T.A. | last post by:
I understand why it is not safe to inherit from STL containers, but I have found (in SGI STL documentation) that for example bidirectional_iterator class can be used to create your own iterator classes by inheriting from it, ie. class my_bidirectional_iterator : public bidirectional_iterator<double> { ... };
5
4037
by: Josef Meile | last post by:
Hi, I have a Map of records and I know how to iterate over it and get the records stored on it. ie: struct featureRecord { featureRecord(CAMERA_FEATURE fId = FEATURE_INVALID_FEATURE, int val = -1) : featureId(fId), value(val) {};
2
3805
by: kjackson.ken | last post by:
I have code such as the following: map<int, stringstringMap; stringMap.insert(map<int,string>::value_type(1, "hello")); map<int, string>::iterator iter; iter = stringMap.find(1);
2
4257
by: subramanian100in | last post by:
I am reading David Musser's "STL Tutorial and Reference Guide" Second Edition. In that book, on pages 68-69, definition has been given that "an iterator can be mutable or constant depending on whether the result of operator* is a reference or a constant reference." As per this definition, on page 71 in this book, it is mentioned that for 'set' and 'multiset', both the iterator and const_iterator types are constant bidirectional types -...
6
7415
by: osama178 | last post by:
Which style is better for initializing iterators: std::vector<int>::iterator iter (aVec.begin() ); or std::vector<int>::iterator iter = aVec.begin() ;
2
2282
by: Terry Reedy | last post by:
Luis Zarrabeitia wrote: Interesting observation. Iterators are intended for 'iterate through once and discard' usages. To zip a long sequence with several short sequences, either use itertools.chain(short sequences) or put the short sequences as the first zip arg. To test without consuming, wrap the iterator in a trivial-to-write one_ahead or peek class such as has been posted before.
5
4571
by: Micheal Smith | last post by:
I'm looking for a good method to convert a map::iterator to a std::string. I've tried googling for a while, but haven't come up with much. Perhaps I'm kludging things here. Either way any advice would be greatly appreciated.
0
8050
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
7987
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
8472
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...
1
8130
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8324
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6805
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...
0
3954
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
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1318
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.