Category: STL Interview Questions
Be careful with hidden cost of std::vector for user defined objects
While creating and using std::vector with user defined classes we needs some special care otherwise it can hamper the performance of our application. [showads ad=inside_post] Let’s learn by example, Suppose we have an Item Class, Advertisements class Item { public: static int m_ConstructorCalledCount; static int m_DestCalledCount; static int m_CopyConstructorCalledCount; Item() { m_ConstructorCalledCount++; } ~Item() { …
Be careful with hidden cost of std::vector for user defined objectsRead More
Remove all occurences of an element from vector in O(n) complexity
Suppose we have a vector of integers and we want to delete all occurences of a number from it i.e. Let’s say vector contain following numbers 1,2,5,4,5,1,5,7,8,9. Now we want to delete all the occurences of 5 from it, so that vector contents should become – 1 2 4 1 7 8 9 . Also …
Remove all occurences of an element from vector in O(n) complexityRead More
How does std::vector works internally ?
std::vector allocates a memory on heap and store all its elements in contiguous memory location. But what if memory it allocated initially is completely filled? For example, let’s create a vector of ints i.e. std::vector<int> . Now suppose it’s initial capacity is to store 10 elements, but in our application we want to store 15 …
C++ std::vector example and why should I use std::vector?
Vector is a template based container that behaves just like a Dynamic Array. It can expands its memory at run time and always store elements in contiguous memory location just like Array. Advertisements We can store any type of element in vector by specifying the type as template argument. [showads ad=inside_post] Frequently Asked: Be careful …
C++ std::vector example and why should I use std::vector?Read More
Importance of Constructors while using User Defined Objects with std::vector
For User Defined classes if Copy Constructor and Assignment Operator are public then only one can insert it’s object in std::vector. This is because of two reasons, Advertisements All STL contains always stores the copy of inserted objects not the actual one. So, whenever we insert any element or object in   container then it’s …
Importance of Constructors while using User Defined Objects with std::vectorRead More
How to use vector efficiently in C++?
We can use vector efficiently by taking care of following points, 1.) Vector will be more efficient if elements are inserted or removed from the back-end only. Advertisements As, vector internally stores all the elements in consecutive memory location. Therefore, if an element is added in middle, then vector right shifts all the right side …
What is std::deque and how deque works internally.
In this article we will discuss what is a deque and how it works internally. Deque is a double ended queue container that provides insertion and deletion at both ends i.e. front and back with high performance unlike vector that provides high performance insertion at end i.e. back only. Advertisements [showads ad=inside_post] Also it provides …
C++ std::list Tutorial, Example and Usage Details
In this article we will discuss std::list usage details. What is std::list ? std::list is sequential STL container that is internally implemented as doubly linked list. i.e. every element in the list is stored at a seperate memory location i.e. called node and it also contains a pointer to the previous and next node. Advertisements …
deque vs vector : What to choose ?
In this article we will discuss the differences between vector and deque STL Containers. Vector provides insertion and deletion at middle and end only. Whereas, deque provides operations for insertion at front, middle and end. That is, apart from push_back() and pop_back() APIs jus like vector, deque also has push_front() and pop_front() API to add …
STL Algorithm – std::sort Tutorial and Example
In this article we will discuss STL Algorithm std::sort algorithm. std::sort is a STL Algorithm which provides the functionality of sorting a given range of elements. To use std::sort we need to pass start and end of range in it as an argument i.e. Advertisements std::sort (<START OF RANGE> , <END OF RANGE>); For example, we …