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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:23:58+00:00 2026-05-17T22:23:58+00:00

I would like to implement a copy of std::stack< boost::shared_ptr<T> > . Is there

  • 0

I would like to implement a copy of std::stack< boost::shared_ptr<T> >. Is there any way to do it without 3 copies? Here is the code:

template<typename T>
void copyStackContent(std::stack< boost::shared_ptr<T> > & dst,
                      std::stack< boost::shared_ptr<T> > const & src){

    //// Copy stack to temporary stack so we can unroll it
    std::stack< boost::shared_ptr<T> > tempStack(src);

    /// Copy stack to array
    std::vector< boost::shared_ptr<T> > tempArray;
    while(!tempStack.empty()){
        tempArray.push_back(tempStack.top());
        tempStack.pop();
    }

    /// Clear destination stack
    while(!dst.empty()){
        dst.pop();
    }

    /// Create destination stack
    for(std::vector< boost::shared_ptr<T> >::reverse_iterator it =
        tempArray.rbegin(); it != tempArray.rend(); ++it){
        dst.push( boost::shared_ptr<T>(new T(**it)) );
    }
}

And a sample test:

void test(){
    // filling stack source
    std::stack< boost::shared_ptr<int> > intStack1;
    intStack1.push( boost::shared_ptr<int>(new int(0)) );
    intStack1.push( boost::shared_ptr<int>(new int(1)) );
    intStack1.push( boost::shared_ptr<int>(new int(2)) );
    intStack1.push( boost::shared_ptr<int>(new int(3)) );
    intStack1.push( boost::shared_ptr<int>(new int(4)) );

    // filling stack dest
    std::stack< boost::shared_ptr<int> > intStack2;
    copyStackContent(intStack2, intStack1);

    assert(intStack1.size() == intStack2.size());         // same size
    while(!intStack1.empty()){
        assert(intStack1.top() != intStack2.top());       // != pointers
        assert((*intStack1.top()) == (*intStack2.top())); // same content
        intStack1.pop();
        intStack2.pop();
    }
}
  • 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-17T22:23:58+00:00Added an answer on May 17, 2026 at 10:23 pm

    If you want to maintain the ordering, you’re kind of stuck since stack doesn’t provide any iterators. If you don’t want to use a deque, you can at least make the code clearer (and more efficient under certain circumstances) by passing the source stack by value:

    template<typename T>
    void copyStackContent(std::stack< boost::shared_ptr<T> > & dst,
                          std::stack< boost::shared_ptr<T> > src){
    
        // Copy stack to array
        std::vector< boost::shared_ptr<T> > tempArray;
        while(!tempStack.empty()){
            tempArray.push_back(tempStack.top());
            tempStack.pop();
        }
    
        // Clear destination stack
        while(!dst.empty()){
            dst.pop();
        }
    
        // Create destination stack
        for(std::vector< boost::shared_ptr<T> >::reverse_iterator it =
            tempArray.rbegin(); it != tempArray.rend(); ++it){
            dst.push( boost::shared_ptr<T>(new T(**it)) );
        }
    }
    

    Though, I am suspicious of copying the values pointed to by shared_ptrs. Why even dynamically allocate if you’re going to be copying everything anyhow?

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

Sidebar

Related Questions

I would like to implement a very simple way to store a variable containing
I have a DataGrid (called TheGrid) that I would like to implement copy and
How would you implement copy-paste support in a RESTful way? Let's say I have
Is there an SVN client with post commit hook? Here's what I would like
I would like to know if there is a way to put only protected
I would like to implement the Ramer–Douglas–Peucker_algorithm in C++. The pseudo code looks like
I would like to implement a decorator that provides per-request caching to any method,
I have some code that moves files around and would like to implement a
I would like to implement a distinct thread for each route in apache camel.I
I would like to implement a function with R that removes repeated characters in

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.