By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,937 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,937 IT Pros & Developers. It's quick & easy.

avoiding copying of iterator to const_iterator

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.