Gice

Technology and General Blog

The array is applied to retail store several data, and the amount of components of the array just cannot be improved at the operate time. This trouble can be solved by working with a vector that performs like a dynamic array. Distinct capabilities exist in the vector course to incorporate and clear away an element from the vector.  The erase() functionality is made use of to eliminate one particular or far more things from the vector at the operate time that decreases the measurement of the vector. The utilizes of this functionality have been stated in this tutorial.

Syntax:

Two forms of syntax of this functionality have given down below.

iterator erase (iterator position)

The earlier mentioned erase() operate is utilized to eliminate a solitary factor from the vector, and it returns an iterator, and it details to the component that is followed by the last erased element.

iterator erase (iterator commencing_situation, iterator ending_position)

The previously mentioned erase() function is employed to clear away various things from the vector based on the position mentioned in this function’s 1st and next arguments.

Pre-requisite:

Right before examining the illustrations of this tutorial, you have to check out the g++ compiler is mounted or not in the technique. If you are using Visible Studio Code, then put in the required extensions to compile the C++ supply code to create the executable code. Listed here, the Visible Studio Code application has been utilised to compile and execute the C++ code. Distinct uses of the erase() purpose to insert component(s) into a vector have revealed underneath.

Instance-1: Take away the 1st ingredient component

Produce a C++ file with the following code to get rid of an ingredient from the starting of the vector. A vector of string values has been employed in the code. The values of the vector have been printed in advance of and immediately after deleting the 1st factor from the vector making use of erase() functionality with a single parameter.

//Include things like essential libraries

#include

#consist of

using namespace std

//Exhibit the values of the vector

void show_vector(vector<string> fruits)

        //Initialize the counter

        int counter = 1

        //Iterate and print the components of the vector applying loop

        for (car ele = fruits.begin() ele != fruits.close() ele++)

       

                //Verify the aspects is the previous component 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

#contain

working with namespace std

//Show the vector

void screen_vector(vector<int> nums)

        //Print the values of the vector working with loop

        for(car ele = nums.begin() ele != nums.conclude() 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 current 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

        //Established the iterator to the 1st place

        startEle = intArray.get started()

        //Increment the commencing iterator by 2

        progress(startEle, 2)

        //Set the iterator to the final place

        endEle = intArray.finish()

        //Decrement the ending iterator by 3

        progress(endEle, 3)

        //Get rid of the variety of aspects

        intArray.erase(startEle, endEle)

        //Print the current values of the vector just after take away

        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

#incorporate

employing namespace std

int primary()

        //Declare a vector of integer knowledge

        vector<int> quantities = 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.

Leave a Reply

Your email address will not be published. Required fields are marked *