Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6342341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:13:24+00:00 2026-05-24T20:13:24+00:00

This is a purely theoretical question, I know that the standard containers interface are

  • 0

This is a purely theoretical question, I know that the standard containers interface are not likely to change now…

I read recently one of Herb Sutter “guru of the week” where he complained about the fact that empty() was implemented as a member function in std::string. I did not agree with all the arguments, because e.g. std::list would require a different implementation of the same function, as size() is O(n) and empty() is obviously O(1).

However, is there a reason why the standard specify that the empty() member function of (for instance) std::string is implemented as "size () == 0" instead of "begin () == end ()" ?

It seems to me that the latter allow the same O(1) implementation of the empty() function for all containers, and I can’t think of any drawback. Is it less efficient?

Thanks,

N.G.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T20:13:25+00:00Added an answer on May 24, 2026 at 8:13 pm

    The standard doesn’t specify exactly how anything in the library is implemented. It merely specifies the meaning of something. So if empty() is true, then size() must also be 0. That doesn’t mean that empty() must actually call std::basic_string::size() and compare it to 0. The spec is simply saying that if empty() returns true, then calling size() immediately after will return 0, and if empty() returned false, then calling size() immediately after will not return 0.

    The spec could have said that its begin() would equal its end(), and it would force no implementations to change.

    The inconsistency is most likely a consequence of the std::basic_string class having come from a different place from the rest of the STL containers during the development of C++98. That’s why it has so many member functions for doing things that STL containers would typically do with algorithms.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is purely a theoretical question, I know that if someone declares a method
This is a purely theoretical question. Given three simple classes: class Base { }
First of all, this question is purely theoretical. Fact is, whether it's possible or
A Purely theoretical question on Tcl. Following this question I was thinking on what
This question is purely theoretical. I was wondering whether the Linux source code could
I'm asking this question purely from a usability standpoint ! Should a website expand/stretch
This is a difficult and open-ended question I know, but I thought I'd throw
Read this question today about safe and unsafe code I then read about it
I am not sure if I can do this purely with sed: I am
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.