468,484 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,484 developers. It's quick & easy.

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 1785
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Alexander Stippler | last post: by
reply views Thread by nick | last post: by
5 posts views Thread by John Harrison | last post: by
1 post views Thread by flopbucket | last post: by
reply views Thread by mailforpr | last post: by
6 posts views Thread by Rares Vernica | last post: by
3 posts views Thread by gallows | last post: by
2 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.