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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:30:05+00:00 2026-06-11T02:30:05+00:00

#include <iostream> #include <cstdlib> #include <ctime> #include <algorithm> #include <vector> using namespace std; struct

  • 0
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <vector>
using namespace std;

struct delete_ptr
{
    template<typename T>
    void operator()(T*& t)
    {
        delete t;
        t = 0;
    }
};

struct is_null_ptr
{
    template<typename T>
    bool operator()(T*& t)
    {
        return t == 0;
    }
};

struct A
{
    static void removeDead(A*& a)
    {
        if(a and a->dead)
            delete_ptr()(a);
    }

    static void killSome(A* a)
    {
        if(a and a->isDead() == false and rand()%100 == 0)
        {
            static int counter = 0;
            cout << "Kill___" << ++counter << endl;
            a->kill();
        }
    }

    static void reviveSome(A* a)
    {

        if(a and a->isDead() and rand()%3 == 0)
        {
            static int counter = 0;
            cout << "Revive___" << ++counter << endl;
            a->revive();
        }
    }

    A():dead(false)
    {

    }

    virtual ~A()
    {
        static int counter = 0;
        cout << "Dtor___" << ++counter << endl;
    }

    bool isDead(){return dead;}
    void kill(){dead = true;}
    void revive(){dead = false;}

    bool dead;
};

int main()
{
    srand(time(0));
    vector<A*> as;
    for(int i = 0; i < 200; ++i)
    {
        A* a = new A;
        as.push_back(a);
    }


    for_each(as.begin(),as.end(),A::killSome);
    for_each(as.begin(),as.end(),A::reviveSome);

    for_each(as.begin(),as.end(),A::removeDead);
    as.erase( std::remove_if(as.begin(),as.end(),is_null_ptr()),as.end());
    cout << as.size() << endl;

    for_each(as.begin(),as.end(),delete_ptr());
    as.clear();

    return 0;
}

It allocates them, and prints the right output but I’m not sure this is the right thing I’m doing. I was just trying to use pointers in a vector and delete them when a certain condition happens, without using boost or c++11.
So what do you think about it?

  • 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-11T02:30:07+00:00Added an answer on June 11, 2026 at 2:30 am

    Since the only smart pointer present in the current STL (auto_ptr) cannot be used in containers, I would say your way is a good one under the given conditions.

    You could think about implementing your own unique_ptr or shared_ptr however.

    PS: There are many reasons to use pointers instead of the actual objects in a container, one is polymorphism. Another one is that the actual objects are already stored somewhere else (think of an index structure to already stored objects).

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

Sidebar

Related Questions

#include <cstdlib> #include<iostream> using namespace std; template <typename T> class stack { public: T
#include <iostream> #include <cassert> #include <vector> #include <ctime> #include <cstdlib> #include <Windows.h> using namespace
#include <iostream> #include <math.h> #include <cstdlib> using namespace std; void circle(int x, int y,
The following code: #include <cstdlib> #include <iostream> using namespace std; int function(void) { static
#include <iostream> #include <string> #include <algorithm> #include <cstdlib> #include <cstdio> using namespace std; static
#include iostream #include conio.h #include exception #include cstdlib using namespace std; void myunexpected ()
#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
Code looks like that #include <cstdlib> #include <iostream> using namespace std; #define n 3;
Example code: #include <cstdlib> #include <iostream> using namespace std; class A { public: A(int
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int FILENAME_MAX=20; int main()

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.