Posts

C++ data structure and containers and their operation with time complexity

C++ provides a rich set of data structures and containers in its Standard Library, particularly within the <vector>, <list>, <deque>, <set>, <map>, <unordered_set>, and <unordered_map> headers. Here is a summary of some of the key data structures, their operations, and the associated time complexities: Vector (`std::vector`) - Access: O(1) for random access using `operator[]` or `at()`. - Insertion/Deletion at End: Amortized O(1) for `push_back()`, O(1) for `pop_back()`. - Insertion/Deletion at Beginning or Middle: O(n). - Search: O(n) for linear search. List (`std::list`) - Access: O(n) for random access (lists do not support direct access). - Insertion/Deletion at Beginning or Middle: O(1) (constant time since it is a doubly linked list). - Search: O(n) for linear search. Deque (`std::deque`) - Access: O(1) for random access using `operator[]` or `at()`. - Insertion/Deletion at End: O(1) for `push_back()` and `pop_back()`. - Inserti...

Copyable and non-copyable Objects in C++

In C++, objects can be categorized as either copyable or non-copyable based on whether they can be copied or assigned using the copy constructor and copy assignment operator. The copyability of an object is determined by the presence or absence of these copy operations. Copyable Objects: Copyable objects can be copied and assigned using the copy constructor and copy assignment operator. By default, user-defined types in C++ are copyable unless specific measures are taken to disable or restrict copying. When an object is copied, a new object is created with the same state as the original object. Example of a copyable class: class Copyable { public: Copyable() {} Copyable(const Copyable& other) {} Copyable& operator=(const Copyable& other) { return *this; } }; Non-Copyable Objects: Non-copyable objects cannot be copied or assigned using the copy constructor and copy assignment operator. This is often intentional when dealing with resources that should not b...

C++11 rvalue and rvalue reference

In C++, an rvalue refers to a temporary value that does not have a persistent identity and is typically created during expressions or as the result of a function call. An rvalue can be a literal (e.g., numeric constant), a temporary object, or the result of an expression evaluation. An rvalue reference, denoted by &&, is a special type of reference that can bind to an rvalue. It was introduced in C++11 and allows for the creation of move semantics, which enables more efficient resource management and improved performance. Here are the key points to understand about rvalues and rvalue references: Rvalue: An rvalue is a temporary value that can be moved from or used to initialize other objects. It does not have a persistent identity and is typically short-lived. Lvalue: An lvalue refers to an object that has a persistent identity and is typically addressable. It can appear on the left side of an assignment operation. Rvalue Reference: An rvalue reference (&&) is a r...

C++11 What is Variadic template and How it works?

Variadic templates are a feature introduced in C++11 that allows functions and classes to take a variable number of arguments of different types. The syntax for a variadic template is to use an ellipsis ... after the type parameter. Here is an example of a variadic function that takes a variable number of arguments and returns their sum: template<typename T> T sum(T value) { return value; } template<typename T, typename... Args> T sum(T value, Args... args) { return value + sum(args...); } The first template function is the base case, which simply returns the value passed in. The second template function is the recursive case, which adds the current value to the sum of the remaining arguments. Variadic templates can also be used to create variadic classes. Here is an example of a variadic tuple class that stores a tuple of values of different types: template<typename... Ts> class Tuple {}; template<typename T, typename... Ts> class Tuple<T,...

C++11 What is Delegating Constructors?

Delegating constructors are a feature introduced in C++11 that allows one constructor of a class to call another constructor of the same class to initialize the object. This feature simplifies code and eliminates the need to repeat the same code multiple times for different constructors. A delegating constructor has the same syntax as a regular constructor, but it calls another constructor of the same class in its member initializer list using the this keyword. Here's an example: class MyClass { public: MyClass(int x, int y) : x_(x), y_(y) {} MyClass(int x) : MyClass(x, 0) {} // delegating constructor private: int x_;  int y_; }; In this example, the MyClass has two constructors: one that takes two int parameters and one that takes one int parameter. The second constructor is a delegating constructor because it calls the first constructor using this . The x parameter is passed to the first constructor, and the y parameter is set to zero. Delegating constructors...

Prepare for your next C++ interview

Preparing for a C++ interview requires a solid understanding of the language, its features, and common programming concepts. Here are some key areas to focus on: C++ basics: Review the fundamentals of C++, such as variables, data types, operators, control structures (if-else, loops), functions, and classes. Pointers and memory management: Understand how pointers work in C++ and be comfortable with concepts like memory allocation and deallocation, dynamic memory allocation (new/delete), and memory leaks. Adopt Smart Pointers. Understand what are they, How they work and know when to use them. Object-oriented programming (OOP): Review the principles of OOP, including encapsulation , inheritance , and polymorphism . Understand the concepts of classes, objects, constructors, destructors, access specifiers, and virtual functions and virtual classes. Templates and generics: Familiarize yourself with C++ templates, including function templates and class templates. Understand how to use the...