wektor erase() i clear() w C++

Warunek wstępny: Wektor w C++

Wektory to to samo, co tablice dynamiczne, z możliwością automatycznej zmiany rozmiaru po wstawieniu lub usunięciu elementu, a ich przechowywanie jest automatycznie obsługiwane przez kontener.

wektor::jasny()

The jasne() funkcja służy do usunięcia wszystkich elementów kontenera wektorów, nadając mu w ten sposób rozmiar 0.

Składnia:

 vector_name .clear() 

Parametry: Żadne parametry nie są przekazywane.

Wynik: Wszystkie elementy wektora są usuwane (lub niszczone).

Przykład:

 Input: myvector= {1, 2, 3, 4, 5}; myvector.clear(); Output: myvector= {} 

C++




// C++ program to demonstrate> // Implementation of clear() function> #include> #include> using> namespace> std;> int> main()> {> > vector <> int> >mójwektor;> > myvector.push_back(1);> > myvector.push_back(2);> > myvector.push_back(3);> > myvector.push_back(4);> > myvector.push_back(5);> > // Vector becomes 1, 2, 3, 4, 5> > myvector.clear();> > // vector becomes empty> > // Printing the vector> > for> (> auto> it = myvector.begin(); it != myvector.end();> > ++it)> > cout < <> ' '> < < *it;> > return> 0;> }>

Wyjście

 No Output 

Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)
Wszystkie elementy ulegają zniszczeniu jeden po drugim.

Błędy i wyjątki

  1. Nie ma wyjątku gwarancji rzutu.
  2. Pokazuje błąd po przekazaniu parametru.

wektor::usuń()

usuwać() funkcja służy do usuwania elementów z kontenera z określonej pozycji lub zakresu.

Składnia:

 vector_name . erase( position ) ; for deletion at specific position vector_name . erase( starting_position , ending_position ) ; // for deletion in range 

Parametry:

  • Pozycja elementu do usunięcia w postaci iteratora.
  • Zakres jest określany za pomocą iteratorów start i end.

Wynik: Elementy są usuwane z określonej pozycji kontenera.

Przykład:

 Input : myvector= {1, 2, 3, 4, 5}, iterator= myvector.begin()+2 myvector.erase(iterator); Output : 1, 2, 4, 5 Input : myvector= {1, 2, 3, 4, 5, 6, 7, 8}, iterator1= myvector.begin()+3, iterator2= myvector.begin()+6 myvector.erase(iterator1, iterator2); Output : 1, 2, 3, 7, 8 

Usuwanie jakiś element z A szczególne stanowisko

Przykład:

C++




// C++ program to demonstrate> // working of erase() function> #include> #include> using> namespace> std;> int> main()> {> > vector <> int> >mójwektor{ 1, 2, 3, 4, 5 };> > vector <> int> >::iterator;> > it = myvector.begin();> > myvector.erase(it);> > // Printing the Vector> > for> (> auto> it = myvector.begin(); it != myvector.end();> > ++it)> > cout < <> ' '> < < *it;> > return> 0;> }>

Wyjście

 2 3 4 5 

Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)

Usunięcie konkretnego elementu

Aby usunąć konkretny element na podstawie jego wartości, najpierw musimy znać jego położenie i możemy go znaleźć za pomocą funkcji find()

Przykład:

C++




// C++ program to remove element based on its value> #include> using> namespace> std;> int> main()> {> > vector <> int> >wektor = { 1, 2, 3, 3, 4, 5 };> > cout < <> 'vector before deleting '> < < endl;> > for> (> auto> element : vector) {> > cout < < element < <> ' '> ;> > }> > // finding the position of the element in the vector> > int> valueToBeDeleted = 3;> > auto> it = find(vector.begin(), vector.end(),> > valueToBeDeleted);> > if> (it != vector.end()) {> > vector.erase(it);> > }> > cout < < endl> > < <> 'Vector after deleting valueToBeDeleted '> > < < endl;> > for> (> auto> element : vector) {> > cout < < element < <> ' '> ;> > }> > cout < < endl;> > return> 0;> }>

Wyjście

vector before deleting 1 2 3 3 4 5 Vector after deleting valueToBeDeleted 1 2 3 4 5 

Usuwanie elementów w zakresie

C++




// C++ program to demonstrate> // Implementation of erase() function> #include> #include> using> namespace> std;> int> main()> {> > vector <> int> >mójwektor{ 1, 2, 3, 4, 5 };> > vector <> int> >::iterator it1, it2;> > it1 = myvector.begin();> > it2 = myvector.end();> > it2--;> > it2--;> > myvector.erase(it1, it2);> > // Printing the Vector> > for> (> auto> it = myvector.begin(); it != myvector.end();> > ++it)> > cout < <> ' '> < < *it;> > return> 0;> }>

Wyjście

 4 5 

Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)

Usuwanie elementów pary wektorów

Przykład:

C++




#include> using> namespace> std;> // Function to print vector pair elements> void> print(vectorint, string>>i rzecz) {cut < < '['; for (int i = 0; i cout < < '{' < < vec[i].first < < ',' < < vec[i].second < < '}'; if (i cout < < ', '; } cout < < ']' < < endl; } int main() { vectorint, string>> x = { { 1, 'jabłko' }, { 2, 'banan' }, { 3, 'wiśnia' }, { 4, 'Gujawa' } }; // Usuń element z pozycji 1 (indeks 0) x.erase(x.begin()); drukuj(x); // Drukuj [{2,banan}, {3,wiśnia}, {4,Gujawa}] // Usuń elementy z pozycji 0 i 1 (indeksy 0 // i 1) x.erase(x.begin(), x.begin() + 2); drukuj(x); // Drukuj [{4,Guava}] // Wyczyść wektor x.clear(); drukuj(x); // Nic nie drukuj (tylko puste nawiasy) return 0; } // Ten kod został stworzony przez Susobhan Akhuli>

Wyjście

[{2,banana}, {3,cherry}, {4,Guava}] [{4,Guava}] [] 

Złożoność czasowa: NA)
Przestrzeń pomocnicza: O(1)

Błędy i wyjątki

  1. Nie ma wyjątku gwarancji rzutu, jeśli pozycja jest ważna.
  2. W przeciwnym razie pokazuje niezdefiniowane zachowanie.

Aplikacja

Mając listę liczb całkowitych, usuń wszystkie elementy parzyste z wektora i wydrukuj wektor.

Wejście:

1, 2, 3, 4, 5, 6, 7, 8, 9 

Wyjście:

1 3 5 7 9 

Wyjaśnienie: 2, 4, 6 i 8, które są parzyste i usunięte z wektora

Algorytm

  1. Uruchom pętlę do rozmiaru wektora.
  2. Sprawdź, czy element na każdej pozycji jest podzielny przez 2, jeśli tak, usuń element i zmniejsz iterator.
  3. Wydrukuj końcowy wektor.

Poniższy program implementuje powyższe podejście.

C++




// C++ program to demonstrate> // Application of erase() function> #include> #include> using> namespace> std;> int> main()> {> > vector <> int> >mójwektor{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };> > for> (> auto> i = myvector.begin(); i != myvector.end();> > ++i) {> > if> (*i % 2 == 0) {> > myvector.erase(i);> > i--;> > }> > }> > // Printing the vector> > for> (> auto> it = myvector.begin(); it != myvector.end();> > ++it)> > cout < <> ' '> < < *it;> > return> 0;> }>

Wyjście

 1 3 5 7 9 

Złożoność czasowa: O(N) w najgorszym przypadku, ponieważ kasowanie zajmuje czas liniowy.

clear() vs Erase(), kiedy czego używać?

jasne() usuwa wszystkie elementy z kontenera wektorów, zmieniając w ten sposób jego rozmiar na 0. Wszystkie elementy wektora są usuwane za pomocą jasne() funkcjonować.

usuwać() natomiast funkcja służy do usuwania określonych elementów z kontenera lub zakresu elementów z kontenera, zmniejszając w ten sposób jego rozmiar o liczbę usuniętych elementów.