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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:49:10+00:00 2026-05-20T22:49:10+00:00

Is this correct?: std::vector<Enemy*> enemies; enemies.push_back(new Enemy()); Enemy* enemy = enemies[0]; enemies.erase(enemies.begin() + 0);

  • 0

Is this correct?:

std::vector<Enemy*> enemies;
enemies.push_back(new Enemy());

Enemy* enemy = enemies[0];
enemies.erase(enemies.begin() + 0);
delete enemy;
  • 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-20T22:49:11+00:00Added an answer on May 20, 2026 at 10:49 pm

    It works, yes, but it’s not an ideal approach.

    Firstly, adding 0 is just noise, you can remove that. But even better, just use pop_front(). Also, no need for the intermediate step, you can delete before removing.

    But std::vector isn’t good as popping from the front, especially if it’s large (because the remaining elements need to be shifted to fill the void). If you don’t need contiguous memory, use a std::deque instead. Or, if order doesn’t matter, you can use something like this:

    template <typename T, typename A>
    void unordered_pop_front(std::vector<T, A>& vec)
    {
        using std::swap;
        swap(vec.front(), vec.back()); // constant time
    
        vec.pop_back(); // constant time
    }
    

    It swaps the front element with the back element, then pops it off. Of course, order is not retained.

    The other problem is with your approach to memory management. Anytime you have explicit clean up code, you’ve done something wrong. It should be done automatically.

    Use either Boost’s ptr_vector, or a std::vector of smart pointers. (Note: do not use std::auto_ptr in a container, it’s broken in this regard.) For a quick smart pointer suggestion, use either std::unique_ptr (if your compiler supports C++0x), or std::/boost::shared_ptr.

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

Sidebar

Related Questions

When invoking the method push_back from std::vector , its size is incremented by one,
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int
Is this valid and correct? RewriteRule ^myOldPage.html$ /index.php#info [R] I'm specifically interested about the
So I wrote this short script (correct word?) to download the comic images from
I'd like to know whether this approach is correct or if their are better
This may not be the correct way to use controllers, but I did notice
This is a double question: The correct answer will go to How you do
What is the correct way to do this? For example, how would I change
What is the correct syntax for this: IList<string> names = Tom,Scott,Bob.Split(',').ToList<string>().Reverse(); What am I
Which would be the correct format for this XML data, are they equivalent or

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.