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

  • SEARCH
  • Home
  • 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 7815203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:26:56+00:00 2026-06-02T05:26:56+00:00

I was surprised to find out the vector::erase move elements on calling erase .

  • 0

I was surprised to find out the vector::erase move elements on calling erase . I thought it would swap the last element with the “to-be-deleted” element and reduce the size by one. My first reaction was : “let’s extend std::vector and over-ride erase()” . But I found in many threads like ” Is there any real risk to deriving from the C++ STL containers? “, that it can cause memory leaks. But, I am not adding any new data member to vector. So there is no additional memory to be freed. Is there still a risk?

Some suggest that we should prefer composition over inheritance. I can’t make sense of this advice in this context. Why should I waste my time in the “mechanical” task of wrapping every function of the otherwise wonderful std::vector class.? Inheritance indeed makes the most sense for this task – or am I missing something?

  • 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-06-02T05:26:59+00:00Added an answer on June 2, 2026 at 5:26 am

    Delicate issue. The first guideline you’re breaking is: “Inheritance is not for code reuse“. The second is: “Don’t inherit from standard library containers”.

    But: If you can guarantee, that nobody will ever use your unordered_vector<T> as a vector<T> you’re good. However, if somebody does, the results may be undefined and/or horrible, regardless of how many members you have (it may seem to work perfectly but nevertheless be undefined behaviour!).

    You could use private inheritance, but that would not free you from writing wrappers or pulling member functions in with lots of using statements, which would almost be as much code as composition (a bit less, though).


    Edit: What I mean with using statements is this:

    class Base {
      public:
        void dosmth();
    };
    
    class Derived : private Base {
      public:
        using Base::dosmth;
    };
    
    class Composed {
      private:
        Base base;
      public:
        void dosmth() {return base.dosmth(); }
    };
    

    You could do this with all member functions of std::vector. As you can see Derived is significantly less code than Composed.

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

Sidebar

Related Questions

Last week I was surprised to find out that sybase 12 doesn't support full
I'm quite surprised I haven't been able to find out what characters I need
I was surprised to find that the following code System.out.println("Character size:"+Character.SIZE/8); System.out.println("String size:"+"a".getBytes().length); outputs
I was surprised to find out that the parameter-less constructor of my base class
Some C or C++ programmers are surprised to find out that even storing an
I've been researching and was surprised to find out that there was no text
In order to find out which element occurs most often in a given array,
I'm still learning C# and was surprised to find out that a List<T> is
I was surprised to find that Go has a 'goto' statement. I've always been
I'm a bit surprised to find the results of the following code, where I

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.