Category: C++
boost::any usage details
What is boost::any : boost::any is a class from Boost::any Library. It provides the ability to store arbitrary information in a variable in C++. Advertisements What to include : Header File – “boost/any.hpp”. Frequently Asked: boost::any usage details boost::any questions What can be stored in boost::any : Anything that is copy constructable like, Built-in data …
Designing a Binary Search Tree Validation class
Many times we need to validate a Binary Search tree i.e. verifying its properties, Left subtree of each node contains the nodes with value less than the node’s value. Right subtree of each node contains the nodes with value greater than the node’s value. Moreover, left and right subtree of each node must be a …
boost::any questions
Question 1: Will this code compile : Advertisements #include <iostream> #include “boost/any.hpp” class Node { private: int value; Node(const Node & obj) { value = obj.value; } public: Node(int i) { value = i; } }; int main() { boost::any var1 = new Node(5); return 0; } Answer: [code collapse=”true”] Yes, because we are storing …
Finding nth Largest and Smallest Node in Binary Seach Tree
Binary Search Tree Concept: In Binary Search Tree, Advertisements When we do the In-Order traversal, nodes will be visited in ascending order.In-Order Traversal For each Node, Traverse the left subtree Visit the node Traverse the right subtree i.e. for above BST, nodes will be visited in following order, 1 , 3 , 4 , 5 …
Finding nth Largest and Smallest Node in Binary Seach TreeRead More
C++11 Smart Pointer – Part 2: shared_ptr and Custom Deletor
In this article we will discuss how to use custom deleter with std::shared_ptr. When a shared_ptr object goes out of scope, its destructor is called. Inside its destructor it decrements the reference count by 1 and if new value of reference count is 0 then it deletes the associated raw pointer. Advertisements To delete the …
C++11 Smart Pointer – Part 2: shared_ptr and Custom DeletorRead More
C++11 Smart Pointer – Part 1: shared_ptr Tutorial and Examples
In this article we will discuss a smart pointer implementation provide by c++11 i.e. shared_ptr. What is std::shared_ptr<> ? shared_ptr is a kind of Smart Pointer class provided by c++11, that is smart enough to automatically delete the associated pointer when its not used anywhere. Thus helps us to completely remove the problem of memory leaks …
C++11 Smart Pointer – Part 1: shared_ptr Tutorial and ExamplesRead More
C++11 Smart Pointer – Part 3: shared_ptr vs Pointer
In this article we will compare C++11 Smart Pointer Implementation shared_ptr and a normal pointer. Let’s compare shared_ptr and raw pointer based on following aspects i.e. Advertisements Missing ++, – – and [] operator In comparison to a raw pointer, shared_ptr provides only these operators 1.) -> , * 2.) Comparison Operators Frequently Asked: C++11 …
C++11 Smart Pointer – Part 3: shared_ptr vs PointerRead More
C++11 Smart Pointer – Part 4: Create shared_ptr objects carefully
We should create shared_ptr objects carefully. Checkout below two cases, [showads ad=inside_post] Advertisements 1.) Try not to use same raw pointer for creating more than one shared_ptr object because in that case different shared_ptr objects will not get to know that they are sharing the pointer with any other shared_ptr objects. How this can create …
C++11 Smart Pointer – Part 4: Create shared_ptr objects carefullyRead More
C++11 Smart Pointer – Part 5: shared_ptr, Binary trees and the problem of Cyclic References
Main advantage of shared_ptr is that it automatically releases the associated memory when not used any more. But if we don’t use shared_ptr carefully then this advantage can turn into a disadvantage. Let’s look how, Advertisements Suppose I have to design a binary tree and in it the node contains a pointer to left and …
C++11 Smart Pointer – Part 5: shared_ptr, Binary trees and the problem of Cyclic ReferencesRead More
Create a Singly Linked List from an array
Suppose we have an int array and we want to create it into a singly linked list from it. Singly Linked List Our Node structure is, Advertisements struct node { int value; node * nextPtr; node(int val) { value = val; nextPtr = NULL; …