Category: std::vector
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 …
C++ : How to find an element in vector and get its index ?
In this article we will different ways to find an element in vector and get its index. Suppose we have a vector of int i.e. Advertisements std::vector<int> vecOfNums = { 12, 45, 54, 33, 2, 7, 8, 22, 43, 19 }; Now we want to find if number 22 exists in vector ? If yes …
C++ : How to find an element in vector and get its index ?Read More
C++ : How to insert element in vector at specific position | vector::insert() examples
In this article we will discuss how to insert one or multiple elements at specific index in vector. Vector provides different overloaded version of member function insert() , to insert one or more elements in between existing elements. Let’s discuss them in detail, Advertisements Inserting a single element at specific position in vector We are …
C++ : How to insert element in vector at specific position | vector::insert() examplesRead More
C++ : How to get element by index in vector | at() vs operator []
In this article we will discuss different techniques to get an element from vector by index or position. In vector elements are indexed from 0 to size() – 1. To access any element in vector by index vector provides two member functions i.e. at() operator[] Let’s discuss them in detail, Advertisements Access an element in …
C++ : How to get element by index in vector | at() vs operator []Read More
How to compare two vectors in C++
In this article, we will discuss different ways to compare two vectors in C++. Table Of Contents Comparing two vectors using operator == Comparing two vectors using STL Algorithm std::equal() Comparing two vectors with custom comparators Summary Best Courses to Learn Modern C++, like C++11, C++17 and C++20 Comparing two vectors using operator == std::vector …
C++ : How to find duplicates in a vector ?
In this article we will discuss how to find duplicate elements in vector and their repetition count. Suppose we have a vector of strings i.e. Advertisements // Vector of strings std::vector<std::string> vecOfStings{ “at” , “hello”, “hi”, “there”, “where”, “now”, “is”, “that” , “hi” , “where”, “at”, “no”, “yes”, “at”}; Let’s find duplicate elements from this …
How to Print a vector in C++?
In this article, we will discuss different ways to print all the elements of a vector in C++. Table of Contents Print a Vector in C++ by overloading << Operator Print a Vector in C++ in comma separated manner Print a Vector in C++ using indexing Print a Vector in C++ in one line without …
How to print Two Dimensional (2D) Vector in C++ ?
In this article, we will discuss different ways to print the contents of a two dimensional vector in C++. Table of Contents Print two dimensional vector using range based for loops C++: Print 2D vector by overloading << operator & using indexing Print 2D Vector / Matrix in one line using lambda inside lambda In …