basic_string::erase

Rimuove un elemento o un intervallo di elementi in una stringa da una posizione specificata.

iterator erase(
    iterator _First, 
    iterator _Last
);
iterator erase(
    iterator _It
);
basic_string<CharType, Traits, Allocator>& erase(
    size_type _Pos = 0,
    size_type _Count = npos
);

Parametri

  • _First
    Un iteratore destinato alla posizione del primo elemento nell'intervallo da cancellare.

  • _Last
    Un iteratore destinato alla posizione una dopo l'ultimo elemento nell'intervallo da cancellare.

  • _It
    Un iteratore destinato alla posizione dell'elemento nella stringa da cancellare.

  • _Pos
    Indice del primo carattere della stringa da rimuovere.

  • _Count
    Il numero di elementi che verranno rimossi se vi sono compresi stringa a partire da _Pos.

Valore restituito

Per le prime due funzioni membro, un iteratore destinato al primo carattere dopo l'ultimo carattere rimosso dalla funzione membro.Per la terza funzione membro, un riferimento all'oggetto stringa i cui elementi sono stati eliminati.

Note

La terza funzione membro restituisce *this.

Esempio

// basic_string_erase.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( ) 
{
   using namespace std;

   // The 1st member function using a range demarcated
   // by iterators
   string str1 ( "Hello world" );
   basic_string <char>::iterator str1_Iter;
   cout << "The original string object str1 is: " 
        << str1 << "." << endl;
   str1_Iter = str1.erase ( str1.begin ( ) + 3 , str1.end ( ) - 1 );
   cout << "The first element after those removed is: "
        << *str1_Iter << "." << endl;
   cout << "The modified string object str1 is: " << str1 
           << "." << endl << endl;

   // The 2nd member function erasing a char pointed to 
   // by an iterator
   string str2 ( "Hello World" );
   basic_string <char>::iterator str2_Iter;
   cout << "The original string object str2 is: " << str2
        << "." << endl;
   str2_Iter = str2.erase ( str2.begin ( ) + 5 );
   cout << "The first element after those removed is: "
        << *str2_Iter << "." << endl;
   cout << "The modified string object str2 is: " << str2 
        << "." << endl << endl;

   // The 3rd member function erasing a number of chars 
   // after a char
   string str3 ( "Hello computer" ), str3m;
   basic_string <char>::iterator str3_Iter;
   cout << "The original string object str3 is: " 
        << str3 << "." << endl;
   str3m = str3.erase ( 6 , 8 );
   cout << "The modified string object str3m is: " 
        << str3m << "." << endl;
}
  
  
  
  
  
  
  
  

Requisiti

intestazione: <string>

Spazio dei nomi: deviazione standard

Vedere anche

Riferimenti

basic_string Class