473,325 Members | 2,480 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,325 software developers and data experts.

Iterator initialization style

Which style is better for initializing iterators:

std::vector<int>::iterator iter (aVec.begin() );

or

std::vector<int>::iterator iter = aVec.begin() ;

Jun 27 '08 #1
6 7399
On Sat, 10 May 2008 21:27:21 +0200, <os******@gmail.comwrote:
Which style is better for initializing iterators:

std::vector<int>::iterator iter (aVec.begin() );

or

std::vector<int>::iterator iter = aVec.begin() ;

For me it's the same thing
Jun 27 '08 #2
On May 10, 12:30*pm, "Alf P. Steinbach" <al...@start.nowrote:
* osama...@gmail.com:
Which style is better for initializing iterators:
std::vector<int>::iterator iter (aVec.begin() );
or
std::vector<int>::iterator iter *= aVec.begin() ;

Yes.

For example.

Anyways, define "better", and do explain why you're limiting the context to
iterators.
Just in terms of style really. Why iterators? Because I am reading an
item in Sutter's Exceptional C++ and noticed that he used both forms
in different examples, for doing virtually the same thing.
Jun 27 '08 #3
Just in terms of style really. Why iterators? Because I am reading an
item in Sutter's Exceptional C++ and noticed that he used both forms
in different examples, for doing virtually the same thing.
That´s probably to show that both ways are sintaticaly correct.

I don´t really think there´s such thing as a consensus on better style
here. The second option is clearer to me, so I would choose it.
Jun 27 '08 #4
On 2008-05-10 21:38, David Côme wrote:
On Sat, 10 May 2008 21:27:21 +0200, <os******@gmail.comwrote:
>Which style is better for initializing iterators:

std::vector<int>::iterator iter (aVec.begin() );

or

std::vector<int>::iterator iter = aVec.begin() ;


For me it's the same thing
For the compiler too.

--
Erik Wikström
Jun 27 '08 #5
In article <fdb8c646-950b-4774-961f-3a04da433b57
@b9g2000prh.googlegroups.com>, os******@gmail.com says...
Which style is better for initializing iterators:

std::vector<int>::iterator iter (aVec.begin() );

or

std::vector<int>::iterator iter = aVec.begin() ;
For most practical purposes, the two are identical. From a purely
theoretical viewpoint, the second is marginally more complex. The first
initializes iter directly from aVec.begin(), where the second
(theoretically) creates a temporary, then copies that temporary into
iter.

In reality, it's probably fairly rare that the compiler will really
create and copy the temporary. Even if it did iterators are easy and
cheap to copy anyway, so it's extremely unlikely to make a difference
anyway.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #6
Jerry Coffin wrote:
In article <fdb8c646-950b-4774-961f-3a04da433b57
@b9g2000prh.googlegroups.com>, os******@gmail.com says...
>Which style is better for initializing iterators:

std::vector<int>::iterator iter (aVec.begin() );

or

std::vector<int>::iterator iter = aVec.begin() ;

For most practical purposes, the two are identical. From a purely
theoretical viewpoint, the second is marginally more complex. The first
initializes iter directly from aVec.begin(), where the second
(theoretically) creates a temporary, then copies that temporary into
iter.
That would be true in general case of copy initialization. But in the particular
case when the type of the object being initialized is the same as the type of
the initializer expression (ignoring cv-qualifications), both forms are
precisely equivalent. The OP's example seems to imply that the types are the
same, which means that there's no difference whatsoever, regardless of the
compiler and its optimization capabilities.

--
Best regards,
Andrey Tarasevich
Jun 27 '08 #7

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

Similar topics

2
by: Frederik Himpe | last post by:
I'm implementing a SAX handler with libxml++. I have this piece of code: void PalamedeParser::on_start_element(const std::string &name, const AttributeList &attributes) { AttributeList::iterator...
11
by: Mateusz Loskot | last post by:
Hi, I have a simple question about naming convention, recommeded names or so in following case. I have a class which is implemented with one of STL container (aggregated) and my aim is to...
6
by: TOMERDR | last post by:
Hi,i am new to stl and i have a question regarding iterators I have class day contains a map of appointments: map<time_t,Appointment> m_Appointments; I would like to write a function...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
1
jlm699
by: jlm699 | last post by:
Greetings friends. I'm trying to create an iterator to iterate over a database of messages. I defined an __iter__() and a next() function as such: import win32com.client as w32c from...
11
by: barcaroller | last post by:
I've noticed that neither container.begin() nor container.end() return an error when the container is empty, so I get a nasty segfault when I dereference the iterator. Do I have to check if the...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
5
by: Luis Zarrabeitia | last post by:
Hi there. For most use cases I think about, the iterator protocol is more than enough. However, on a few cases, I've needed some ugly hacks. Ex 1: a = iter() # assume you got the iterator...
3
by: Gianni Mariani | last post by:
So I was put straight that double(v) is the same as (double)(v) recently and I'm somewhat surprised. I'm not the only one. I think T(V) should have exactly the same semantics as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.