The erase function is prototyped as follows:
(1) string& erase (size_t pos = 0, size_t n = NPOs);
(2) Iterator erase (iterator position);
(3) Iterator erase (iterator first, iterator last);
That means there are three ways to use:
(1) Erase (pos,n); Deleting n characters starting from POS, such as erase (0,1), deletes the first character
(2) Erase (position); Deletes a character at position (position is a string type of iterator)
(3) Erase (first,last); Delete the characters from first to last (both first and last are iterators)
Let me give you an example:
#include<iostream>
#include<String>
UsingNamespaceStd
IntMain ()
{
StringSTR ("This was an example phrase.");
String:: Iterator it;
//Section (1) Usage
Str.erase (10,8);
cout<<Str<<Endl//"This was an phrase."
//Section (2) Usage
It=Str.begin ()+9;
Str.erase (IT);
cout<<Str<<Endl//"This is a phrase."
//Section (3) Usage
str.erase (Str.begin () +5, str.end () - 7);
cout << str < Span style= "color: #000000;" ><< endl; // "this phrase."
Return 0}
The function of the erase () function is used to remove elements from the container
Delete an element in a container: C.erase (T);
Seemingly a simple action, but for different types of containers, the interior has done a very different thing, the following introduction. Suppose you have a topic that removes all the elements in a container that satisfy the condition n = = x, and you should have a code like this as usual:
//Suppose that container and container represent a container and an object of its respective
Container<T>:: Iterator it;
For(It container.begin (); it != container.end (); ++it) {
if== x)
Container.erase (IT);
}
However, such code is wrong for either container
Containers can be divided into linked list containers and array containers by memory allocation method.
The so-called linked list container refers to a form of representation, including a list, slist, such as a node-based container (dynamic allocation of memory blocks) and set, map, Multiset, Multimap and other associated containers (balanced tree implementation), The array container refers to a contiguous memory container that holds elements on a contiguous memory, such as vectors, deque, strings, and so on.
As an example of list container, when executing container.erase (it), it is true that the first element that satisfies the condition has been deleted, but the IT pointer has been deleted and it does not point to any element, so it can only end here. That is, the above code for the linked list container can only correctly delete the first element that satisfies the condition, the first thing we think about is to do a backup before deleting the pointer.
This temporary variable is built directly into the erase implementation, which is more concise and professional.
List<Int>:: Iterator it;
For(It=Lt.begin (); It!=Lt.end (); ) {
If(*It% 0 )
lt.erase ( It + + ) ; // Here is the key
else
++it;
}
A list container using erase to delete a node also has a feature that will return the address of the next element, so it can be implemented as well:
List<Int>:: Iterator it;
For(It=Lt.begin (); It!=Lt.end (); ) {
If(*It%2 = = 0)
It = lt.erase (it);//automatically returns the address of the next element without actively moving the pointer forward
Else
+ +it;
}
Vector<Int>:: Iterator it=V.begin ();
For(It=V.begin (); It!=V.end (); ) {
If(*It%2==0)
v.erase (it);//After elements are removed, the following elements move forward automatically, without moving the fingers
else
it;
}
Online There is said in VS2005 inside the v.erase (it) writing is the line vs2008 and 2010 but run error will appear
Vector erase iterator outside range The most insurance option is to change v.erase (it) to it=v.erase (it)
Use of the erase function in C + + can be used to remove memory erasure