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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:30:22+00:00 2026-05-17T19:30:22+00:00

#include <boost/ptr_container/ptr_vector.hpp> #include <iostream> using namespace std; class Derived { public: int i; Derived()

  • 0
#include <boost/ptr_container/ptr_vector.hpp>
#include <iostream>
using namespace std;

class Derived
{
public:
        int i;
        Derived() {cout<<"Constructed Derived"<<endl;}
        Derived(int ii):i(ii) {cout<<"Constructed Derived"<<i<<endl;}
        ~Derived() {cout<<"* Destructed Derived"<<i<<endl;}
};

int main()
{
   boost::ptr_vector<Derived> pv;
   for(int i=0;i<10;++i) pv.push_back(new Derived(i));

   boost::ptr_vector<Derived>::iterator it;
   for (it=pv.begin(); it<pv.end();/*no iterator increment*/ )
        pv.erase(it);
   cout<<"Done erasing..."<<endl;
}

Notice that the second for loop does not increment the iterator, yet it iterates and erases all elements. My questions are:

  1. Is my technique of iteration and using the iterator correct?
  2. If iterator increment is not required in the for loop, then where does the increment happen?
  3. Is it better to use an iterator or will an ordinary integer suffice (ie: is there any value-add with using iterators)? (coz I can also erase the 5th element like pv.erase(pv.begin()+5);)
  4. Is there any way to assign a new object to a specific position (let’s say the 5th position) of ptr_vector, directly? I’m looking for something like pv[5]=new Derived(5);. Any way of doing that?
  • 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-17T19:30:23+00:00Added an answer on May 17, 2026 at 7:30 pm

    Is my technique of iteration and using the iterator correct?

    No, erasing from a container generally invalidates the iterator to the erased item. If it works, this is just a side-effect of the implementation details.

    The correct way would be to use the return value of the erase method:

     it = pv.erase(it);
    

    However, for emptying the container, you can use the clear member function.

    If iterator increment is not required in the for loop, then where
    does the increment happen?

    It doesn’t happen, because you’ll always be erasing the first item in the container (by chance, might not work out with other containers).

    Is it better to use an iterator or will an ordinary integer suffice (ie:
    is there any value-add with using
    iterators)? (coz I can also erase the
    5th element like
    pv.erase(pv.begin()+5);)

    In a random-access container you can do that, otherwise not (such as a list).

    Is there any way to assign a new object to a specific position (let’s
    say the 5th position) of ptr_vector,
    directly? I’m looking for something
    like pv[5]=new Derived(5);. Any way
    of doing that?

    According to the boost reference:

    pv.replace(5, new Derived(5));
    

    This returns the existing pointer in a smart pointer, so it will be automatically deallocated.
    (It’s curious that this takes an index, not an iterator…).

    Or:

    pv[5] = Derived(5);
    

    but this will just modify the stored object, not change the pointer.

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

Sidebar

Related Questions

#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream> using namespace std; int main() { double u = 0; double w
#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) {
#include iostream class A { private: int a; public : A(): a(-1) {} int
#include <vector> std::vector<long int> as; long int a(size_t n){ if(n==1) return 1; if(n==2) return
Whenever I include boost in my project I get a million of these warnings.
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
include(conn.php); $result = mysql_query(SELECT * FROM sggame); while($row = mysql_fetch_assoc($result)); { $id = $row['id'];
include 'header.php'; // ... some code header('Location:index.php'); exit; The above code keeps giving me

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.