empty vs. cont.begin() == cont.end()

G

gelbeiche

Is
( cont.begin() == cont.end() )
essentially equivalent to writing
( cont.empty() )
for a STL container ?
 
C

CrayzeeWulf

gelbeiche said:
Is
( cont.begin() == cont.end() )
essentially equivalent to writing
( cont.empty() )
for a STL container ?

Yes. According to the standard the operational semantics of the related
expressions are:

a.size() => a.end() - a.begin()
a.empty() => a.size() == 0

q.e.d
 
M

Mike Wahler

gelbeiche said:
Is
( cont.begin() == cont.end() )
essentially equivalent to writing
( cont.empty() )
for a STL container ?

Yes, but there's a good possiblity that using 'empty()'
could be (fractionally) faster because it might be
optimized to simply report the value of a flag instead
of performing a comparison. However, such a 'performance'
difference would likley not be detectable in most situations.
I personally prefer to use 'empty()' for the simple reason that
it's imo more descriptive than 'begin() == end()' (and it's
less typing, too. :) )

-Mike
 
G

gelbeiche

Mike Wahler said:
Yes, but there's a good possiblity that using 'empty()'
could be (fractionally) faster because it might be
optimized to simply report the value of a flag instead
of performing a comparison. However, such a 'performance'
difference would likley not be detectable in most situations.
I personally prefer to use 'empty()' for the simple reason that
it's imo more descriptive than 'begin() == end()' (and it's
less typing, too. :) )
Ok, thanks. That was the answer and remarks I expected.
 
J

Jeff Flinn

CrayzeeWulf said:
Yes. According to the standard the operational semantics of the
related expressions are:

std::map said:
a.size() => a.end() - a.begin()

Doesn't compute! Not all containers provide random access iterators.
a.empty() => a.size() == 0

!q.e.d

When you want to know if a container is empty, use c::empty. Assume that the
implementer will use the most efficient implementation for the container.

Jeff Flinn
 
C

CrayzeeWulf

Jeff said:
Doesn't compute! Not all containers provide random access iterators.

True. That is why they are operational semantics only and not necessarily
the way to implement the corresponding methods. However, if "a.end() -
a.begin()" is allowed, then it is logically equivalent to "a.empty()".

Thanks,
 
C

CrayzeeWulf

Jeff said:
Doesn't compute! Not all containers provide random access iterators.

True. That is why they are operational semantics only and not necessarily
the way to implement the corresponding methods. However, if "a.end() -
a.begin()" is allowed, then it is logically equivalent to "a.empty()".

Note that the original post was about "a.begin() == a.end()" which will work
for all iterators. As stated in ISO/IEC 14882:2003(E) Section 23.1(para#7):

"begin() returns an iterator referring to the first element in the
container. end() returns an iterator which is the past-the-end value for
the container. If the container is empty, then begin() == end();"

Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top