In this C++ tutorial, we will go through some of the most used Vector Operations with examples.
C++ Vector Operations
Vectors are containers that can store elements in order, and can change in size.
We can perform operations on C++ Vector, like CRUD operations, conversions from vector to other datatypes or vice versa, etc.
Create
The following list of tutorials cover scenarios of how to create a vector.
- C++ Create an empty vector C++ Tutorial to create empty vectors like empty integer vector, empty string vector, etc.
- C++ Create vector of specific size C++ Tutorial to create a vector of given size.
- C++ Create vector with initial values C++ Tutorial to create a vector and initialize the same with given values.
- C++ Copy a vector to another C++ Tutorial to copy a original vector to another using simple Assignment Operator.
- C++ Vector length or size C++ Tutorial to find the length of given vector.
- C++ Vector of vectors C++ Tutorial to define vector of vectors, where each element of the outer vector is a vector, and how to traverse the elements of inner vector.
Access
We can access the elements of a vector. The following tutorials show how to access elements using an index, or iterate in a loop, etc.
- C++ Vector – Print elements C++ Tutorial to print the elements of a vector, one by one, to console output.
- C++ Vector – Iterate using For loop C++ Tutorial to iterate over the elements of a vector using for loop.
- C++ Vector – Iterate using While loop C++ Tutorial to iterate over the elements of a vector using while loop.
- C++ Vector – Foreach C++ Tutorial to execute a block of code for each element in the given vector.
- C++ Vector – Get reference to element at specific index C++ Tutorial to get reference to an element at specific index in the given vector.
Checks
The following tutorials cover different checks that we can make on a vector.
- C++ Check if vector is empty C++ Tutorial to check if the given vector is empty, i.e., with no elements.
- C++ Check if two vectors are equal C++ Tutorial to check if given two vectors are equal.
- C++ Check if element is present in vector C++ Tutorial to check the vector contains specified element.
Update or Transform
The following tutorials cover update or remove operations at element level on a vector.
- C++ Add element(s) to vector C++ Tutorial to add one or more elements to a vector.
- C++ Append element to the end of vector C++ Tutorial to append an element at the end of a vector using
vector::push_back()
method. - C++ Append vector to another vector C++ Tutorial to append the elements of a vector to another vector using
vector::insert()
method. - C++ Insert element at the beginning of vector C++ Tutorial to insert an element at the beginning of vector, using
vector::insert()
method. - C++ Remove first element from vector C++ Tutorial to remove the first element of a vector.
- C++ Remove last element from vector C++ Tutorial to remove the last element of a vector.
- C++ Remove element(s) at specific index(es) from vector C++ Tutorial to remove elements from given indices.
- C++ Remove duplicates from a vector C++ Tutorial to remove the duplicates of a vector by iterating over the vector elements, and checking if it has occurred previously in the vector.
- C++ Remove elements from a vector based on a condition C++ Tutorial to remove elements from a given vector, based on a condition. The tutorial covers examples like: 1. delete negative elements from vector, 2. delete empty strings from vector.
- C++ Resize vector C++ Tutorial to resize the vector, i.e., increase or decrease the size of the vector to a given size.
- C++ Swap elements of two vectors C++ Tutorial to swap elements between two vectors.
- C++ Remove all elements from vector C++ Tutorial to remove all the elements from the given vector, and make it empty.
- C++ Reverse a vector C++ Tutorial to reverse a vector using
std::reverse()
method ofalgorithm
library. - C++ Sort a vector C++ Tutorial to sort a vector using
std::sort()
method ofalgorithm
library.
Conversions
The following tutorials cover conversions from vector to other types, and from other types to vector.
- C++ Convert array to vector C++ Tutorial to convert a given array of elements into a vector.
- C++ Convert vector to map C++ Tutorial to convert a given vector of elements to a map with elements as keys, and their number of occurrences as values.
- C++ Join elements of vector to a string C++ Tutorial to join the elements (integers or strings) of given vector by a delimiter string.
Vector Programs
- C++ Filter even numbers in an integer vector C++ Tutorial to filter only even numbers in an integer vector.
- C++ Filter odd numbers in an integer vector C++ Tutorial to filter only odd numbers in an integer vector.
- C++ Get unique elements of a vector C++ Tutorial to take a vector with duplicate elements, remove duplicates from the vector, and keep only the unique elements.
- C++ Remove empty string elements from a string vector C++ Tutorial to delete empty strings from a vector by comparing each of the string element with an empty string.
- C++ Sort integer vector in ascending order C++ Tutorial to sort elements of an integer vector in ascending or increasing order.
- C++ Sort integer vector in descending order C++ Tutorial to sort elements of an integer vector in descending or decreasing order.
- C++ Sort string vector based on length C++ Tutorial to sort a string vector based on the length of strings (elements) by using a comparison function with sort() function.
- C++ Sort string vector lexicographically C++ Tutorial to sort a string vector based on their order in the respective language Dictionary.
- C++ Split vector into two equal halves C++ Tutorial to split a given vector into two halves. The first half is a vector from beginning to mid way of the original vector. The second half is a vector from mid way to end of the original vector.
Conclusion
In this C++ Tutorial, we have gone through Vector Operations, with detailed tutorials.