Vector methods in C++
What is a vector in c++?
In C++, a vector is a container that holds a sequence of elements. It is a template class in the Standard Template Library (STL). Vectors are similar to arrays but can grow and shrink dynamically, allowing them to be used in situations where the number of elements is not known in advance.
To use vectors in C++, you must include the <vector>
header in your program.
Here is an example of how to create and use a vector:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
// Create a vector that holds integers
vector<int> vec;
// Add some elements to the vector
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// Print the elements in the vector
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i] << std::endl;
}
return 0;
}
Output:

Explanation:
The above is used to demonstrate vectors in c++.In this program, we have created a vector of integers and added three elements. Then, we printed the elements in the vector using a loop. We are using the vector header file to create a vector in the program.
In C++, many different methods can be used with vectors. Here are some examples of common vector methods and how to use them:
- push_back(): Adds an element to the end of the vector. This method takes a single argument, which is the element to be added. For example:
#include <vector>
#include <iostream>
int main()
{
// Create a vector that holds integers
std::vector<int> vec;
// Add some elements to the vector
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// Print the elements in the vector
for (int i = 0; i < vec.size(); i++)
{
std::cout << vec[i] << std::endl;
}
return 0;
}
Output:

Explanation:
The above program demonstrates the using push the back method in vector. This program creates a vector of integers and uses the push_back()
method to add three elements. Then, it prints the elements in the vector using a loop. The push_back()
method is useful for adding elements to a vector's end. It takes a single argument, which is the element to be added, and it adds the element to the end of the vector. In this example, the program adds the numbers 1, 2, and 3 to the vector.
pop_back()
: Removes the last element from the vector. This method does not take any arguments and returns the removed element. For example:
#include <vector>
#include <iostream>
int main()
{
// Create a vector that holds integers
std::vector<int> vec;
// Add some elements to the vector
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
// Remove the last element
vec.pop_back();
for (int i = 0; i < vec.size(); i++)
{
std::cout << vec[i] << std::endl;
}
return 0;
}
Output:

Explanation:
The above program is used to demonstrate the pop-back method in vectors. In this program, we have created a vector of integers and used the push_back()
method to add three elements. Then, it uses the pop_back()
method to remove the last element from the vector. In the output, we can see that only the first two elements are printed. This is because the pop-back method has removed the last inserted element.
Size ()
: Returns the number of elements in the vector. This method does not take any arguments, and it returns an integer. For example:
#include <iostream>
#include <vector>
int main()
// Create a vector with 10 elements
std::vector<int> numbers(10);
// Set the values of the vector elements
for (int i = 0; i < numbers.size(); i++)
numbers[i] = i;
// Print the size of the vector
std::cout << "The size of the vector is: " << numbers.size() << std::endl;
return 0;
}
Output:

Explanation:
The size()
function returns the number of elements in the vector. In this case, the vector numbers
have 10 elements, so numbers. Size ()
will return 10. Note that you can also use the size()
function to obtain the size of a vector when iterating through it, as shown in the for
loop in the above example. This can be useful when you want to process all the elements in a vector without knowing the exact size of the vector in advance.
- Insert(): The
insert()
method adds elements to a vector at a specified position. This method can be very useful when you want to insert an element into the middle of a vector rather than just adding it to the end, for example.
#include <vector>
#include <iostream>
int main()
{
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
numbers.push_back(15);
numbers.insert(numbers.begin() + 1, 20);
for (int n : numbers)
{
std::cout << n << std::endl; // Outputs 5, 20, 10, 15
}
return 0;
}
Output:

Explanation:
In this example, the insert()
method adds the number 20 to the numbers
vector at the second position (index 1). The first argument to the insert()
method is the iterator pointing to the position where the new element should be inserted. The second argument is the value of the new element.
After the insert()
method is called, the numbers
vector will contain the following elements: 5, 20, 10, and 15. This shows that the new element was inserted at the correct position in the vector.
- Erase(): The
erase()
method is a function to remove elements from a vector at a specified position. This method can be very useful when removing an element from the middle of a vector rather than just removing the last element. For example
#include <vector>
#include <iostream>
int main()
{
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
numbers.push_back(15);
numbers.erase(numbers.begin() + 1);
for (int n : numbers)
{
std::cout << n << std::endl; // Outputs 5, 15
}
return 0;
}
Output:

Explanation:
In this example, the erase()
method is used to remove the second element from the numbers
vector (which is the number 10). The argument to the erase()
method is the iterator pointing to the position of the element to be removed.
After the erase()
method is called, the numbers
vector will contain the following elements: 5, 15. This shows that the element at index 1 was removed from the vector.
- Clear (): The
clear()
method is a function that removes all elements from a vector. This method can be very useful when you empty a vector and reuse it for a different purpose. For example
#include <vector>
#include <iostream>
int main()
{
std::vector<int> numbers;
numbers.push_back(5);
numbers.push_back(10);
numbers.push_back(15);
numbers.clear();
std::cout << numbers.empty() << std::endl; // Outputs 1 (true)
return 0;
}
Output:

Explanation :
The clear() method is called on the numbers vector in this example. This removes all elements from the vector, leaving it empty. You can check if a vector is empty by using the empty()
method, which returns true
(1) if the vector is empty and false
(0) otherwise.
- Empty(): The
empty()
method checks if a vector is empty. This method returnstrue
if the vector has no elements andfalse
otherwise. For example
#include <vector>
#include <iostream>
int main()
{
std::vector<int> numbers;
std::cout << numbers.empty() << std::endl; // Outputs 1 (true)
numbers.push_back(5);
numbers.push_back(10);
numbers.push_back(15);
std::cout << numbers.empty() << std::endl; // Outputs 0 (false)
numbers.clear();
std::cout << numbers.empty() << std::endl; // Outputs 1 (true)
return 0;
}
Output:

Explanation:
The empty() method is called on the numbers vector three times in this example. The first time, it is called on an empty vector, so it outputs 1 (true). The second time, it is called on a vector with three elements, so it outputs 0 (false). The third time, it is called on an empty vector again after calling the clear()
method to remove all elements from the vector, so it outputs 1 (true) again.
- Resize(): In the C++ programming language, the
resize()
method is used to change the size of a vector. This method can be used to either increase or decrease the size of the vector. When the size of the vector is increased, new elements are added to the vector, and their values are default-initialized. When the size of the vector is decreased, the excess elements at the end of the vector are removed.
Example:
#include <iostream>
#include <vector>
int main()
{
// Create a vector of integers with an initial size of 5
std::vector<int> numbers(5);
// Set the values of the elements in the vector
for (int i = 0; i < 5; i++)
{
numbers[i] = i;
}
// Print the size and contents of the vector
std::cout << "Vector size: " << numbers.size() << std::endl;
std::cout << "Vector contents:";
for (int i = 0; i < numbers.size(); i++)
{
std::cout << " " << numbers[i];
}
std::cout << std::endl;
// Resize the vector to have a size of 10
numbers.resize(10);
// Print the size and contents of the vector after resizing
std::cout << "Vector size: " << numbers.size() << std::endl;
std::cout << "Vector contents:";
for (int i = 0; i < numbers.size(); i++)
{
std::cout << " " << numbers[i];
}
std::cout << std::endl;
return 0;
}
Output:

Explanation:
In this example, we create a vector of integers with an initial size of 5. We then set the values of the elements in the vector and print the size and contents of the vector. Next, we use the resize()
method to increase the size of the vector to 10, and we print the size and contents of the vector again to see the effect of the resize()
method.