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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:33:37+00:00 2026-05-23T07:33:37+00:00

Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector

  • 0

Please consider the following piece of code:

int main()
{
    typedef boost::ptr_vector<int> ptr_vector;

    ptr_vector vec0;
    vec0.push_back(new int(1));
    vec0.push_back(new int(2));
    vec0.push_back(new int(3));
    vec0.push_back(new int(4));
    vec0.push_back(new int(5));

    ptr_vector::iterator last  = boost::prior(vec0.end()),
                         first = boost::prior(last, 3);

    ptr_vector vec1(first, last); // this will copy '2, 3, 4' to vec1

    struct print
    {
        void operator()(int const& i) {
            std::cout << i.m_i << std::endl;
        }
    };

    std::for_each(vec0.begin(), vec0.end(), print());   // 1, 2, 3, 4, 5
    std::for_each(vec1.begin(), vec1.end(), print());   // 2, 3, 4

    return 0;
}

I don’t want to copy the elements into vec1, but sharing in a way that shared_ptr<> provides. My requirements basically are:

  • Sharing the same instances of a range of objects, which are part of a container, in another container instance
  • Don’t want to share one instance in more than these two containers
  • Want to be notified when one element is “suddenly” erased from the other container (or at least get check this, e.g. something like shared_ptr::unique())

Both containers are part of the same class. So, they have the same scope and will be destroyed at the same time. The constructor of these class constructs both containers. After construction there won’t be any modification to these containers.

Do I need to use a std::vector<> of shared_ptr<> instead or is there any other solution?

  • 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-23T07:33:38+00:00Added an answer on May 23, 2026 at 7:33 am

    Yes, you should use a vector<shared_ptr<int>>.

    Since you’re only working with ranges, you could put together a custom solution that keeps track of the ranges and their intersections (for implementing unique.) Then, you could store everything in vector and have the ranges index into that. This would probably be faster (just on account of the avoided cache misses), but it would be more work to implement.

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

Sidebar

Related Questions

Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Please consider following code: 1. uint16 a = 0x0001; if(a < 0x0002) { //
Please consider the following snippet: public interface MyInterface { public int getId(); } public
Please consider the following: <td style=width: 500px;> <div style=width: 400px;>SomeContent</div> </td> For some reason,
Please consider the following scenario: map(T,S*) & GetMap(); //Forward decleration map(T, S*) T2pS =
Please consider the following scenario in BizTalk 2006: Received message should be routed to
Please consider the following Amount value type property which is marked as a nullable
I would like to understand the ServiceBehavior.ConcurrencyMode property. Consider the following code on service
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public

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.