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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:50:35+00:00 2026-05-27T03:50:35+00:00

How does one access unique_ptr elements of a container (via an iterator) without taking

  • 0

How does one access unique_ptr elements of a container (via an iterator) without taking ownership away from the container? When one gets an iterator to an element in the container is the element ownership still with the container? How about when one dereferences the iterator to gain access to the unique_ptr? Does that perform an implicit move of the unique_ptr?

I find I’m using shared_ptr a lot when I need to store elements in a container (not by value), even if the container conceptually owns the elements and other code simply wishes to manipulate elements in the container, because I’m afraid of not being able to actually access the unique_ptr elements in the container without ownership being taken from it.

Any insights?

  • 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-27T03:50:35+00:00Added an answer on May 27, 2026 at 3:50 am

    As long as you don’t try to make a copy of the unique_ptr, you can just use it. You’ll have to “double dereference” the iterator to get to the pointer’s value, just as you would have to with shared_ptr. Here’s a brief example:

    #include <vector>
    #include <memory>
    #include <iostream>
    
    template <class C>
    void
    display(const C& c)
    {
        std::cout << '{';
        if (!c.empty())
            std::cout << *c.front();
        for (auto i = std::next(c.begin()); i != c.end(); ++i)
            std::cout << ", " << **i;
        std::cout << "}\n";
    }
    
    int main()
    {
        typedef std::unique_ptr<int> Ptr;
        std::vector<Ptr> v;
        for (int i = 1; i <= 5; ++i)
            v.push_back(Ptr(new int(i)));
        display(v);
        for (auto i = v.begin(); i != v.end(); ++i)
            **i += 2;
        display(v);
    }
    

    If you do (accidentally) make a copy of the unique_ptr:

    Ptr p = v[0];
    

    then you’ll find out at compile time. It won’t cause a run time error. Your use case is why container<unique_ptr<T>> was built. Things should just work, and if they don’t, the problem appears at compile time instead of run time. So code away, and if you don’t understand the compile time error, then ask another question back here.

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

Sidebar

Related Questions

This is a continuation of the post How does one access a method from
How does one access WinForm controls such as ProgressBar properties from another class? Please
Where does one access the data from a parsed xml file when using libxml?
How does one access data imported from a CSV file by using dynamic note
How does one access a particular widget from within a custom layout while using
Using Python, how does one parse/access files with Linux-specific features, like ~/.mozilla/firefox/*.default ? I've
How does one add a comment to an MS Access Query, to provide a
How does one convert an image from one color profile to another (screen to
How does one access command line flag (arguments) as environment variables in Erlang. (As
My question is about how to access the TabBarController from within one of its

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.