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 4551238
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:36:28+00:00 2026-05-21T16:36:28+00:00

I’m reviewing a piece of code and see a class where an std::vector is

  • 0

I’m reviewing a piece of code and see a class where an std::vector is stored as a member variable and the size of that std::vector is stored as a separate member variable. Both std::vector and its “stored copy” of size are never change during the containing object lifetime and the comments say size is stored separately “for convenience and for cases when an implementation computes the size each time“.

My first reaction was “WT*? Should’t it be always trivial to extract std::vectors size?”

Now I’ve carefully read 23.2.4 of C++ Standard and can’t see anything saying whether such implementations are allowed in the first place and I can’t imagine why it would be necessary to implement std::vector in such way that its current size needs non-trivial computations.

Is such implementation that std::vector::size() requires some non-trivial actions allowed? When would having such implementation make sense?

  • 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-21T16:36:29+00:00Added an answer on May 21, 2026 at 4:36 pm

    I would guess that your definition of “trivial” doesn’t match that of the author of the code.

    If size isn’t stored, I’d expect begin and end to be stored, and size to be computed as the difference of the two, and that code to be inlined. So we’re basically talking two (nearby) memory accesses and a subtraction, instead of one memory access.

    For most practical purposes, both of those are trivial, and if the standard library author thinks that the result of that computation isn’t worth caching, then personally I am happy to accept their opinion. But the author of that code comment might think otherwise.

    IIRC the standard says somewhere that size “should” be O(1), not sure if that’s in the text for sequences or for containers. I don’t think it anywhere specifies that it must be for vector. But even if we read that as a non-requirement there’s a fundamental QOI issue here – what on earth am I doing optimizing my code for such a poor implementation at the expense of normal implementations?

    If someone uses such an implementation, presumably that’s because they want their code to run slowly. Who am I to judge otherwise? 😉

    It’s also possible that the author of the code has profiled using a number of end-begin implementations, and measured a significant improvement by caching the size. But I think that’s less likely than that the author is being too pessimistic about the worst case their code needs to handle.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.