C++

How to use unique_ptr with Arrays in C++?

In Modern C++, we can create unique_ptr object to manage dynamically allocated arrays. However, the syntax for using std::unique_ptr with arrays is a bit different. In this article, we will look into this in more detail. Syntax to use unique_ptr with Arrays When you want a create a unique_ptr object to manage a dynamically allocated […]

How to use unique_ptr with Arrays in C++? Read More »

How to pass unique_ptr into a Function in C++?

Unique pointers (std::unique_ptr) is a Smart Pointer and was introduced in C++11. It provide exclusive ownership of a dynamically allocated memory. Unlike regular pointers or std::shared_ptr, std::unique_ptr ensures there is only one owner for the allocated memory, making it a safe and efficient way to handle dynamic memory in C++. In this article, we will

How to pass unique_ptr into a Function in C++? Read More »

How to Return unique_ptr from a function in C++?

Returning a unique_ptr from a function is a common and recommended practice when you want to transfer ownership of dynamically allocated objects without the risk of memory leaks. Here is how you can return a unique_ptr from a function in C++. Returning a unique_ptr Directly C++ supports Return Value Optimization (RVO) and move semantics, which

How to Return unique_ptr from a function in C++? Read More »

Reset unique_ptr in Modern C++

When working with unique_ptr in C++, it’s essential to know how to renounce the ownership of the managed object and free the associated memory when necessary. The unique_ptr class provides the reset() member function for this purpose. The unique_ptr::reset() Function Calling the reset() method on a unique_ptr will perform two operations: Example of reset() function

Reset unique_ptr in Modern C++ Read More »

Introduction to Smart Pointers in Modern C++

Smart Pointers were introduced by C++11 Standard. Smart pointers are a significant advancement that addresses the longstanding challenge of memory management in C++ programming. The Problem of Memory Management Traditionally, managing memory in C++ has involved two primary operations: allocating memory with the new operator and releasing it using the delete operator. This manual process

Introduction to Smart Pointers in Modern C++ Read More »

Iterate over a Set in Reverse Order in C++

This tutorial will discuss multiple ways to iterate over a set in reverse order in C++. Table Of Contents Using reverse_iterator Using C++20 Ranges Library Using reverse_iterator In STL, the set class provides a reverse iterator, named reverse_iterator. Using this iterator, we can iterate over the set elements in reverse order. The set function rbegin()

Iterate over a Set in Reverse Order in C++ Read More »

Find the distance between two Iterators in a Set in C++

This tutorial will discuss how to find the distance between two iterators in a set in C++. In C++, if you have a set containing various numbers and you want to determine the distance between two specific iterators, you can use the std::distance() function from the Standard Template Library (STL). For instance, suppose you have

Find the distance between two Iterators in a Set in C++ Read More »

Scroll to Top