Category: std::vector
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
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
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 …
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
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 …
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 …
How to add an element in Vector using vector::push_back
In this article we will discuss how to add element in a vector. std::vector provides a member function to push an element at the end i.e. Advertisements void push_back (const value_type& val); It will push the element in the end of list. As, this function adds the element, so it also increases the size of …
How to add an element in Vector using vector::push_backRead More