Vector Erase() Function in C++

The array is used to store multiple data, and the number of elements of the array can’t be changed at the run time. This problem can be solved by using a vector that works like a dynamic array. Different functions exist in the vector class to add and remove an element from the vector.  The erase() function is used to remove one or more elements from the vector at the run time that decreases the size of the vector. The uses of this function have been explained in this tutorial.

Syntax:

Two types of syntax of this function have given below.

iterator erase (iterator position);

The above erase() function is used to remove a single element from the vector, and it returns an iterator, and it points to the element that is followed by the last erased element.

iterator erase (iterator starting_position, iterator ending_position);

The above erase() function is used to remove multiple elements from the vector based on the position mentioned in this function’s first and second arguments.

Pre-requisite:

Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code. Different uses of the erase() function to insert element(s) into a vector have shown below.

Example-1: Remove the first element element

Create a C++ file with the following code to remove an element from the beginning of the vector. A vector of string values has been used in the code. The values of the vector have been printed before and after deleting the first element from the vector using erase() function with one parameter.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

//Display the values of the vector

void display_vector(vector<string> fruits)

{

        //Initialize the counter

        int counter = 1;

        //Iterate and print the elements of the vector using loop

        for (auto ele = fruits.begin(); ele != fruits.end(); ele++)

        {

                //Check the elements is the last element or not

                if(counter != fruits.size())

                        cout << *ele << ", ";

                else

                        cout << *ele;

                //Increment the counter by 1

                counter++;

        }

        cout << endl;

}

int main(void) {

        //Declare a vector of string data

        vector<string> fruits = { "Orange", "Banana", "Mango", "Jack Fruit", "Lichi" };

        //Print the existing values of the vector

        cout << "The values of the vector before remove: " << endl;

        display_vector(fruits);

        //Remove the first element from the vector

        fruits.erase(fruits.begin());

        //Print the existing values of the vector after remove

        cout << "\nThe values of the vector after remove: " << endl;

        display_vector(fruits);

        return 0;

}

Output:

The following output will appear after executing the above code.

Example-2: Remove multiple elements

Create a C++ file with the following code to remove the multiple elements from the vector using erase() function. A vector of integer values has been used in the code. Two iterators have been used here to set the range of elements removed from the vector. The erase() function has been used with two parameters to remove multiple elements from the vector.

//Include necessary libraries

#include <vector>

#include <iostream>

using namespace std;

//Display the vector

void display_vector(vector<int> nums)

{

        //Print the values of the vector using loop

        for(auto ele = nums.begin(); ele != nums.end() ; ele++)

             cout << *ele << " ";

        //Add new line

        cout << "\n";

}

int main() {

        //Declare a vector of integer data

        vector <int> intArray { 678, 435, 960, 231, 800, 387, 634, 267, 409, 294};

        //Print the existing values of the vector

        cout << "The values of the vector before remove: " << endl;

        display_vector(intArray);

        //Declare two iterators to remove the range of elements from the vector

        vector<int> :: iterator startEle, endEle;

        //Set the iterator to the first position

        startEle = intArray.begin();

        //Increment the starting iterator by 2

        advance(startEle, 2);

        //Set the iterator to the last position

        endEle = intArray.end();

        //Decrement the ending iterator by 3

        advance(endEle, -3);

        //Remove the range of elements

        intArray.erase(startEle, endEle);

        //Print the existing values of the vector after remove

        cout << "\nThe values of the vector after remove: " << endl;

        display_vector(intArray);

        return 0;

}

Output:

The following output will appear after executing the above code. According to the range of the iterators, the elements from the 3rd position to the 7th position of the vector have been removed.

Example-3: Remove the specific elements

Create a C++ file with the following code to remove the specific elements of the vector using erase() function. A vector of 7 integer elements has been declared in the code. Next, the ‘for’ loop has used to iterate the vector elements and remove those elements from the vector that are not divisible by 5.

//Include necessary libraries

#include <iostream>

#include <vector>

using namespace std;

int main()

{

        //Declare a vector of integer data

        vector<int> numbers = { 78, 45, 67, 25, 98, 75, 52 };

        cout << "The values of the vector before remove:\n";

        for (int const &i: numbers) {

        cout << i << ' ';

        }

        cout << endl;

        //Remove the numbers from the vector those are not divisible by 5

        for (auto ele = numbers.begin(); ele != numbers.end(); ele++)

        {

                //Check the number is divisible by 5 or not

                if (*ele % 5 != 0)

                {

                        //Remove the element based on the iterator

                        numbers.erase(ele--);

                }

        }

        cout << "The values of the vector after remove:\n";

        for (int const &i: numbers) {

                cout << i << ' ';


        }


        cout << endl;


        return 0;

}

Output:

The following output will appear after executing the above code. The output shows that the vector contains the divisible elements by 5 only, and other elements have been removed.

Conclusion:

Different uses of erase() function have been shown in this tutorial to remove vector elements. The C++ has many other functions to remove the element from the vector, and those functions can remove the single element at a time. But both single and multiple elements from any position of the vector can be removed by using the vector erase() function.



from Linux Hint https://ift.tt/3zKJ6dR

Post a Comment

0 Comments