Skip to main content

std::unique_ptr in c++ | C++ 11

std::unique_ptr :

C++11 introduces std::unique_ptr, defined in the header .

It is a container for a raw pointer. A unique_ptr explicitly prevents copying of its contained pointer (as would happen with normal assignment), but the std::move function can be used to transfer ownership of the contained pointer to another unique_ptr.

Reason for it won’t allow copy :
A unique_ptr cannot be copied because its copy constructor and assignment operators are explicitly deleted.

std::unique_ptr p1(new int(5));
std::unique_ptr p2 = p1; //Compile error.
std::unique_ptr p3 = std::move(p1); //Transfers ownership. p3 now owns the memory and p1 is set to nullptr.

p3.reset(); //Deletes the memory.

p1.reset(); //Does nothing.



Complate Example: 


// C++ program to illustrate the use of unique_ptr
#include
#include
using namespace std;
class A
{
public:
    void show()
    {
        cout<<"A::show()"<
    }
};
int main()
{
    unique_ptr p1 (new A);
    p1 -> show();
    // returns the memory address of p1
    cout << p1.get() << endl;
    // transfers ownership to p2
    unique_ptr p2 = move(p1);
    p2 -> show();
    cout << p1.get() << endl;
    cout << p2.get() << endl;
    // transfers ownership to p3
    unique_ptr p3 = move (p2);
    p3->show();
    cout << p1.get() << endl;
    cout << p2.get() << endl;
    cout << p3.get() << endl;
    return 0;
}



Output:

A::show()
0x1c4ac20
A::show()
0          // NULL
0x1c4ac20
A::show()
0          // NULL
0          // NULL
0x1c4ac20

Comments

Popular posts from this blog

Containers in c++

SEQUENTIAL CONTAINER Vector (Dynamic Array - Contiguous memory):  O(1): Vectors provide fast (constant time) element insertion and deletion at the end of the vector and Acess. O(n): Slow (linear time) insertion and deletion anywhere else. Insertion and deletion are slow because the operation must move all the elements “down” or “up” by one to make room for the new element or to fill the space left by the deleted element. Like arrays, vectors provide fast (constant time) access to any of their elements. List (Doubly Linked List - Not  Contiguous memory ): O(n): Lists provide slow (linear time) element lookup and access, O(1):  (constant time) insertion and deletion of elements once the relevant position has been found Deque (Doubly Ended Queue - Not Contiguous memory): O(1): (constant time) element access. Like a list, it provides fast (amortized constant time) insertion and deletion at both ends of the sequence. O(1):  (lin...

Sequence Containers: Vectors

Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container. Container properties Sequence Dynamic array Allocator-aware Example : #include #include int main () { std::vector< int > myvector; for ( int i=1; i<=5; i++) myvector.push_back(i); std::cout << "myvector contains:" ; for (std::vector< int >::iterator it = myvector.begin() ; it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n' ; return 0; }

Part1 : STL Algorithms (Non-Modifying sequence) in c++

Non-Modifying sequence operations : 1.  Non-modifying sequence operations:   _of (CPP 11) std::array all_of_elem = { 3,5,7,11,13,17,19,23 }; 1. if ( std::all_of ( all_of_elem.begin(),   all_of_elem.end() ,  [](int i) {return i % 2; } )) std::cout << "All the elements are odd numbers.\n"; std::array any_of_elem = { 0,1,-1,3,-3,5,-5 }; 2. if ( std::any_of ( any_of_elem.begin() ,  any_of_elem.end() ,  [](int i) {return i )) std::cout << "There are negative elements in the range.\n"; std::array foo = { 1,2,4,8,16,32,64,128 }; 3. if ( std::none_of ( foo.begin() ,  foo.end() ,  [](int i) {return i )) std::cout << "There are no negative elements in the range.\n"; 2.  Non-modifying sequence operations:   find       std::string myints[] = { "Hello", "Hi", "Bye", "ByeBye" }; std::vector myvector(myints, myints + 4); std::vector ::iterator it;...