Erase-Take away Idiom in C++ – GeeksforGeeks

Erase-Take away Idiom in C++ – GeeksforGeeks

Erase-Take away-Idiom is a C++ STL (Customary Template Library) approach to take away parts from a container. It’s used to take away all the weather satisfying sure situations from the container.

The erase-remove idiom is particularly helpful in array-based containers like vectors, the place every elimination requires all of the remaining parts to regulate. Within the worst case, this may increasingly result in O ( n2 ) time complexity. This system avoids this by offering the elimination of the weather in a single parse. That’s the reason, the erase-remove idiom supplies O( n ) time complexity.

On this approach, we use the mix of two member capabilities of the container to take away the weather effectively.

  • std::erase
  • std::take away or std::remove_if

It is because of the usage of this operate that this method known as the erase-remove idiom.

std::erase

The std::erase operate is used to take away parts from the container giving the iterator its place. It will probably additionally take away the vary of parts when the iterator to beginning place and ending place are offered.

Syntax of std::erase

container.erase(place); // to remove single component
        or
container.erase(starting_position, ending_position); // to remove a spread of parts

Right here,

  • place: It’s the iterator to the component to be eliminated.
  • starting_position: iterator to the start line of the vary to be eradicated.
  • ending_position: It’s the iterator to the ending level of the vary to be eradicated.

std::take away

 The std::take away operate is used to relocate the weather which equate to the given worth and return the iterator to the brand new logical finish.

Syntax of std::take away

container.take away(first, final, val);

Right here,

  • first: It’s the iterator to the primary component of the vary the operate has to work.
  • final: It’s the iterator to the final component of the vary.
  • val: It’s the worth to be examined in opposition to.

std::remove_if

The std::remove_if operate is the variation of the take away operate by which we will move a comparator operate as a substitute of a price to check.

Syntax of std::remove_if

container.remove_if(first, final, val);

Right here,

  • first: It’s the iterator to the primary component of the vary the operate has to work.
  • final: It’s the iterator to the final component of the vary.
  • predicate: It’s the operate that specifies the comparator.

Working of Erase Take away Idiom

It is a two-step approach that entails utilizing each std::take away and std::erase capabilities one after one other. The next are the steps:

  • STEP 1: The std::take away operate strikes all parts to be eliminated on the finish of the container. It returns the brand new logical finish of the vary.
  • STEP 2: The std::erase operate is then used to erase all the weather after the brand new logical finish directly.

Implementation of Erase Take away Idiom

The next instance demonstrates the right way to use Erase Take away Idiom for eradicating odd numbers from a vector container.

C++

#embrace <algorithm>

#embrace <iostream>

#embrace <vector>

  

void printV(std::vector<int>& v)

{

    for (auto i : v) {

        std::cout << i << " ";

    }

  

    std::cout << std::endl;

}

  

int principal()

{

    

    std::vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };

  

    

    std::cout << "Unique Vectorttt: ";

    printV(v);

  

    

    

    auto new_logical_end = std::remove_if(

        v.start(), v.finish(), [](int a) { return a % 2; });

  

    

    std::cout << "After utilizing remove_if()t: ";

    printV(v);

  

    

    v.erase(new_logical_end, v.finish());

    std::cout << "After utilizing erase()tt: ";

    printV(v);

}

Output

Unique Vector            : 1 2 3 4 5 6 7 8 9 
After utilizing remove_if()    : 2 4 6 8 5 6 7 8 9 
After utilizing erase()        : 2 4 6 8 

Benefits of Erase Take away Idiom

  • Extremely environment friendly as all of the objects may be eliminated in a single parse.
  • Secure and versatile attributable to the usage of STL predefined operate templates.

Limitations of Erase Take away Idiom

  • It solely works with sure forms of containers like vectors, lists, and deques, and never with others like maps and units.
  • Disrupts the order of remaining parts.
  • It might be sluggish if the predicate operate is pricey.