C++ offers a number of tools for handling data structures including vectors. One of the common tasks is taking elements out of it when working with them. In this blog article, we'll go through the various C++ techniques for erasing or removing items from vectors.
But Let's first revise what vectors are actually!
What are Vectors?
A vector in C++ is a dynamic array that may be resized at runtime. Vectors and arrays are similar in that they may both store a collection of items of the same data type. Vectors, on the other hand, provide more flexibility than arrays since their size may be adjusted at runtime. These are part of the STL which can be accessed via the vector header file.
Vectors are declared using the following syntax:
std::vector<datatype> vector_name;
Also, here are the different methods to initialize vectors in C++.
How to clear a vector in C++?
There are two meanings when we say removing elements from a vector. Either we mean to remove all the elements in the vector or we want to remove only a specific element in a vector.
Now, if you want to remove all the elements, then we use a function called clear(). Clear() is a member function of the vector class in C++ that provides functionality to remove all the elements from a vector.
The syntax for using the clear() function is:
vector_name.clear();
For example, to clear all the elements from a vector of integers, you would write:
#include #include using namespace std; int main() { std::vector<int> my_vector = {1, 2, 3, 4, 5}; cout << "The vector contains " << my_vector.size() << " elements: "<<endl; // print the vector after clearing my_vector.clear(); cout << "The vector contains " << my_vector.size() << " elements: "<<endl; for (auto element : my_vector) { cout << element << " "; } cout << std::endl; return 0; }
Output:
The vector contains 5 elements: The vector contains 0 elements:
We saw in this example that after calling the vector clear() method all the elements in the vector were removed and no output is generated.
The clear() function of the C+ has a time complexity of O(n), where n is the number of elements. It will take a lot of time to remove more and more elements. This is because the function will destroy all the free memory occupied by the elements.
What is the erase() function in vectors?
The erase function in C++ is a member function of the vector class that removes specific elements from a vector rather than removing all the elements in the vector. The erase function either takes a single position of the element you wish to remove or takes a range of all the positions from which you wish to remove the elements.
The syntax for using the erase() function is:
vector_name.erase(position);
or
vector_name.erase(starting_position, ending_position);
The first syntax removes the element at the specified position in the vector. The second syntax removes all the elements between the starting_position and ending_position, including the element at starting_position but not the element at ending_position.
For example, to remove the second element from a vector of integers, you would write:
#include #include using namespace std; int main() { std::vector<int> my_vector = {1, 2, 3, 4, 5}; cout<<"Elements before deleting"<<endl; for (auto element : my_vector) { cout << element << " "; } my_vector.erase(my_vector.begin() + 1); cout<<endl<<"Elements after deleting"<<endl; for (auto element : my_vector) { cout << element << " "; } cout << std::endl; return 0; }
Output:
Elements before deleting 1 2 3 4 5 Elements after deleting 1 3 4 5
We see that the second element was removed when we called the erase() function, the vector will contain the elements {1, 3, 4, 5}. In order to remove the second element we passed an argument “my_vector.begin() + 1”, here my_vector.begin() gives the address of the first element in the vector. Adding one to that points to the second element in the vector.
The erase() function of C++ also has a time complexity of O(n), where n is the number of elements that are to be removed. This means that it will take a significant amount of time. Even erasing a single element has the same complexity because it will shift all the elements after the erased element(s) one position to the left, which requires a lot of copying or moving.
What is better: Erase vs Clear? While both are member functions of the C++ std::vector container, they are used for different purposes. The clear() function is used to remove all the elements, while the erase() function is used to remove one or more elements.
How to remove an element by value?
In the previous method, we saw how to remove elements by providing their positions but what if we want to delete an element but do not know its position? Here we can use a combination technique.
You can apply a combination of the find() and erase() methods to delete an element from a vector by value. In C++, the find() method is a part of the algorithm library. It is used to find a value among a set of items. The syntax for using the find() function is:
std::find(starting_position, ending_position, value);
The iterator returned by the find() method points to the first element in the range that matches the supplied value. If the value cannot be retrieved, the iterator pointing to the ending position is returned.
For example, to remove the element 3 from a vector of integers, you would write:
#include #include #include using namespace std; int main() { std::vector<int> my_vector = {1, 2, 3, 4, 5}; cout<<"Elements before deleting"<<endl; for (auto element : my_vector) { cout << element << " "; } auto it = std::find(my_vector.begin(), my_vector.end(), 3); if (it != my_vector.end()) { my_vector.erase(it); } cout<<endl<<"Elements after deleting"<<endl; for (auto element : my_vector) { cout << element << " "; } cout << std::endl; return 0; }
Output:
Elements before deleting 1 2 3 4 5 Elements after deleting 1 2 4 5
After calling the erase() function, the vector will contain the elements {1, 2, 4,5}.
The find() method is used in this example to locate the iterator pointing to the element with value 3. If the iterator returned by find() is not equal to the ending position, it means that the value was found in the vector. After that, the iterator is handed to the erase() method, which removes the element from the vector.
It's important to remember that if the vector contains several items with the same value, just the first occurrence will be eliminated. To delete all occurrences of a value from a vector, use a loop to run the find() and erase() methods repeatedly until the value is no longer found in the vector.
Conclusion
In C++ programming, removing items from vectors is a typical operation. We learned here about the clear and erase functions for vectors in C++. The clear() method removes all items from a vector, whereas the erase() function removes one or more elements based on location. Happy Learning :)