473,775 Members | 2,625 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Is
( cont.begin() == cont.end() )
essentially equivalent to writing
( cont.empty() )
for a STL container ?
Jul 23 '05 #1
5 2184
gelbeiche wrote:
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
--
CrayzeeWulf
Jul 23 '05 #2

"gelbeiche" <bo*****@fliege npilz.de> wrote in message
news:m3******** ****@redrat.qua rk.de...
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
Jul 23 '05 #3
CrayzeeWulf wrote:
gelbeiche wrote:
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:


std::map<int,in t> a;
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
Jul 23 '05 #4
Jeff Flinn wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #5
Jeff Flinn wrote:
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,
--
CrayzeeWulf
Jul 23 '05 #6

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

Similar topics

4
3071
by: Tran Tuan Anh | last post by:
Dear all, I am new in C++, and now get confused about a lot of things. I wrote this simple code to test the vector. class Temp { public: int x; }; int main() { vector<Temp> v;
3
3596
by: Markus Dehmann | last post by:
I have a two different value types with which I want to do similar things: store them in the same vector, stack, etc. Also, I want an << operator for each of them. class Value{}; // this would be "public interface Value{}" in Java! class IntValue : public Value{ private: int _value; public:
25
7595
by: Matthias | last post by:
Hi, I am just reading that book by Scott Meyers. In Item 4 Meyers suggests to always use empty() instead of size() when probing for emptyness of STL containers. His reasoning is that size() might take linear time on some list implementations. That makes sense at first. However, he also says this at the very beginning: "That being the case , you might wonder why one construct should be preferred to the other, especially in view of the...
3
4435
by: Old Wolf | last post by:
Is this undefined behaviour: #include <string> #include <vector> #include <algorithm> int main() { std::string s; char buf;
14
1600
by: Xero | last post by:
Hello. I am using Visual Studio .NET (Academic Edition) to write a VB program. My computer is running Win XP Pro. I am writing a calculator and requires users to enter two numbers. After entering the numbers, they should click a button called 'Display Results'. How can I display a dialog box prompting the users to check their entry if they left any of the boxes empty? I have tried the .IsNaN function but in vein. Thanks.
7
2100
by: Abhishek Saksena | last post by:
Hi all, Can somebody help me what the bhaviour of front method on an empty map. For some reason calling front on an empty map seems to work with gcc compiler but not with .NET. Abhishek
7
3470
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function 'UpdateRegistrant' expects parameter '@EMail', which was not supplied. The field value was null in the database and not changed in the FormView so is null going back into the stored procedure. I'm stumped and would greatly appreciate any suggestions.
12
2205
by: robertino | last post by:
Hi all, I've put together a few SPs to produce a BOM (bill of materials) listing, which together use a couple of global temp tables, and return the results from a cursor. Here's the code: -- Initialize the temp tables............ create procedure myschema.init_ebom_tables ( ) language sql
2
4857
by: barcaroller | last post by:
If an STL container is empty, what will its begin() member function return? Would it be a valid value that an iterator could use, like the end() member function? In particular, can I assume that begin() == end()?
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10268
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10107
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10048
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9916
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5360
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2853
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.