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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:01:56+00:00 2026-05-31T08:01:56+00:00

I’m trying to implement my own version of vector class without using iterators. Here

  • 0

I’m trying to implement my own version of vector class without using iterators. Here is the parts which may be needed for the question.

template <typename T>
class Vector {
public:
    ...

    ~Vector()
    {
        delete [] m_data;
    }

    ...

    void erase(size_t position)
    {
        if (position >= m_size) {
            throw std::out_of_range("erasing an element out of bounds");
        }
        --m_size;
        for (size_t i = position; i < m_size; ++i) {
            m_data[i] = m_data[i + 1];
        }
        m_data[m_size].T::~T();
    }

    ...

private:
    T* m_data;
    size_t m_size;
    ...
};

Below is a quote from cplusplus.com for erase function of std::vector:

This effectively reduces the vector size by the number of elements removed, calling each element’s destructor before.

So I tried to implement the same functionality by calling the destructor of the last duplicate element. The destructor of m_data[position] is unnecessary as it will be replaced by the next element.

The problem is that the code in the destructor of vector class delete [] m_data will also call the destructors for each element which will cause double deletion of memory and crash.

Can anyone help to write the correct erase function for my vector class?

  • 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-31T08:01:57+00:00Added an answer on May 31, 2026 at 8:01 am

    What you are trying doesn’t fly that easily! If you want to implement something like std::vector<T> you need to do the full monty: you need to deal with raw memory and construct/destroy objects explicitly. That is, you need to allocate a sufficient chunk of uninitialized memory, construct/destroy object at their appropriate locations as needed, and eventually release the allocated memory. It is an interesting exercise to do for toy version of std::vector<T> and then you’ll gladly use version shipping with your compiler because it somehow managed to be faster, actually implement all of the functionality, and is reasonably bug free. Of course, if you happen to implement a version of the standard C++ library you’ll need to suffer through the entire exercise. The good news is: std::vector<T> is trivial compared to std::deque<T> which I’d be prepared to bet big bucks you won’t get anywhere nearly as efficient as a standard library version without the use of algorithms (and to get this really efficient you’d need fairly complex version of the algorithms as well; I’m not sure if there are many implementations which actually do specialized versions which are good on std::deque<T>).

    Not using iterators for this is, BTW, just not helpful: algorithms like std::move() (the version taking iterators as arguments) or std::copy() (if you don’t use C++2011) avoid littering your code with duplicated versions. Having the code in algorithms has the added advantage that they not entirely trivial logic is nicely encapsulated as needed. Putting the repeatedly needed code into algorithms makes the implementation of the containers comparatively simple, giving the implementation a much better chance of being correct. … not to mention that it is actually viable to implement interesting optimizations as well.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am doing a simple coin flipping experiment for class that involves flipping a

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.