Category: STL
Iterating over a range of User Defined objects and calling member function using std::for_each
In this article we will discuss how to iterate over a container of user defined objects and call a member function on each of the iterating element.Suppose you have a vector of Employee class objects and you want to call a member function on each of the element in vector. [showads ad=inside_post] Let’s see how …
c++ std::vector and Iterator Invalidation example
In this article we will discuss the iterator invalidation with respect to std::vector in C++. What is Iterator Invalidation? An Iterator becomes invalidate when the container it points to changes its shape internally i.e. move elements from one location to another and the initial iterator still points to old invalid location. Advertisements Iterator invalidation in …
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 …
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 …
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 …
std::generate Tutorial and example
In this article we will see how to use std::generate and std::generate_n with STL sequential containers. std::generate is a STL algorithm useful in generating data for a sequential container by calling passed function object continuously. For example, Advertisements Suppose, we have a structure Task and a std::vector<Task * > and we want to initialize this …
Creating a Matrix using 2D vector in C++ – Vector of Vectors
In this article will discuss how to create 2D Matrix using vector of vectors in c++. Requirement Advertisements Represent a 2D Matrix using vector in C++ i.e. 1 , 1 , 1 , 1 1 , 1 , 1 , 1 1 , 1 , 1 , 1 1 , 1 , 1 , 1 …
Creating a Matrix using 2D vector in C++ – Vector of VectorsRead More
Different Ways to Initialize a vector in C++
In this article we will discuss different ways to initialize a std::vector in C++. Initializing std::vector elements with same value std::vector<int> vec_1(5, 10); It will create a vector of 5 element and initialize  each element is initialized to 10. Advertisements Initializing std::vector elements with passed argument list std::vector<int> vec_2 = {10,20,30,40,50}; It will create a vector of 5 …
STL Algorithm std::unique Tutorial
In this example we will discuss std::unique STL Algorithm. std::unique is a n STL Algorithms that provide a functionality to remove all consecutive duplicate elements from a given range. Advertisements For example, we have an array of elements i.e. 1,2,3,3,3,4,4,5,5 Frequently Asked: STL Algorithm std::unique Tutorial As we can see above there are 4 consecutive …