Tag: Vector
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 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 …
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 …
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
5 Different ways to Initialize a vector in c++
In this article we will discuss different ways to initialize a vector in C++. Creating a vector object without any initialization will create an empty vector with no elements i.e. Advertisements std::vector<int> vecOfInts; But we generally want to initialize a vector with huge values and calling push_back() that many times to add element in vector is not an …
How to fill a vector with random numbers in C++
In this article we will discuss how to fill a std::vector with random numbers using std::generate. For this task we will use a STL algorithm std::generate i.e. Advertisements template<typename _FIter, typename _Generator> void generate(_FIter start, _FIter end, _Generator gen); It will update all the elements from range start to end -1 with values generated by …
Difference between Vector and List in C++
In this article we will discuss the differences between std::vector and std::list in C++. Both vector and list are sequential containers of C++ Standard Template Library. But there are many differences between them because of their internal implementation i.e. Advertisements List stores elements at non contiguous memory location i.e. it internally uses a doubly linked …
How to copy all Values from a Map to a Vector in C++
In this article we will discuss how to fetch all values from a map and put them in vector. Suppose we have a map of words and their frequency count i.e. Advertisements // Map of string & int // To store the frequency count of words std::map<std::string, int> wordMap = { { “is”, 6 }, …
How to copy all Values from a Map to a Vector in C++Read More