473,232 Members | 1,561 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,232 software developers and data experts.

avoiding copying of iterator to const_iterator

Consider the code fragment:

vector<intcontainer;
container.push_back(0);
container.push_back(1);
container.push_back(2);

Now I want to iterate through the 'container'.

For this, instead of writing,

for (vector<int>::const_iterator cit = container.begin();
cit != container.end();
++cit)
// ... do something ...

I thought I should write:

const vector<int>& ref = container;

for (vector<int>::const_iterator cit = ref.begin();
cit != ref.end();
++cit)
// ... do something ...

The reason is :
in the line vector<int>::const_iterator cit = container.begin();
the 'container' is non-const. So 'container.begin()' will return
plain iterator which is copied to const_iterator. So a
conversion is involved here.

Instead, if I use ref.begin() - with 'ref' as defined above,
'begin() const' will be called on 'ref' which will directly return
a const_iterator. So no conversion is involved here.

Is my understanding correct ?

Also, kindly clarify if the line
const vector<int>& ref = container;
involves any temporary object ?

Thanks
V.Subramanian
Jan 8 '08 #1
2 1934
su**************@yahoo.com, India wrote:
Consider the code fragment:

vector<intcontainer;
container.push_back(0);
container.push_back(1);
container.push_back(2);

Now I want to iterate through the 'container'.

For this, instead of writing,

for (vector<int>::const_iterator cit = container.begin();
cit != container.end();
++cit)
// ... do something ...

I thought I should write:

const vector<int>& ref = container;

for (vector<int>::const_iterator cit = ref.begin();
cit != ref.end();
++cit)
// ... do something ...

The reason is :
in the line vector<int>::const_iterator cit = container.begin();
the 'container' is non-const. So 'container.begin()' will return
plain iterator which is copied to const_iterator. So a
conversion is involved here.
Why bother? The original is idiomatic, the second is not.

Any conversion will be trivial and optimised. std::vector iterator may
well be a plain pointer.
Also, kindly clarify if the line
const vector<int>& ref = container;
involves any temporary object ?
No.

--
Ian Collins.
Jan 8 '08 #2
On 2008-01-12 15:39:57 -0500, Jerry Coffin <jc*****@taeus.comsaid:
In article <2dcde89f-4fb7-4c4d-b811-6ec47a61fbe7
>>
Instead, if I use ref.begin() - with 'ref' as defined above,
'begin() const' will be called on 'ref' which will directly return
a const_iterator. So no conversion is involved here.

Yes and no -- the line that obtains the iterator no longer uses a
conversion, but the line that forms the reference _does_ -- and it's
basically the same conversion. IOW, you've taken simple, easily
recognizable code and converted it to something more complex and less
idiomatic, but retained the same basic problem.
Containers in C++0x will have member functions cbegin(), cend(),
crbegin(), and crend() for exactly this problem. They return
const_iterators, even from non-const containers.

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

Jan 12 '08 #3

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

Similar topics

3
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the...
8
by: Alexander Stippler | last post by:
Hello, the standard requires a type reverse_iterator. But in the form it is required and implemented for all compilers I know, there is IMO something missing, which is essential for its usage....
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
5
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const...
1
by: flopbucket | last post by:
Hi, For the learning experience, I am building a replacement for std::map. I built a templated red-black tree, etc., and all the basic stuff is working well. I implemented basic iterators and...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
21
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...
6
by: Rares Vernica | last post by:
Hi, I am using tr1/unordered_map in my code. My code compiled ok on g++ (GCC) 4.1.2 (Ubuntu 4.1.2-0ubuntu1). Now I need to compile my code on g++ (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu5). I get...
3
by: gallows | last post by:
The container is: template <typename T> class Container { public: // container methods.. // iterator: class const_iterator { public:
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
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...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
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...

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.