logo

C++ のベクトル Erase() および Clear()

前提条件: C++ のベクトル

ベクターは動的配列と同じで、要素が挿入または削除されると自動的にサイズを変更する機能があり、そのストレージはコンテナーによって自動的に処理されます。



ベクトル::クリア()

クリア() 関数を使用してベクター コンテナーのすべての要素を削除し、サイズを 0 にします。

構文:

 vector_name .clear()>

パラメーター: パラメータは渡されません。



結果: ベクターのすべての要素が削除 (または破棄) されます。

例:

 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>>私のベクトル;>>' 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;> }>

>

>

出力

 No Output>

時間計算量: の上)
補助スペース: ○(1)
すべての要素が 1 つずつ破壊されます。

エラーと例外

  1. 例外スローがないことが保証されています。
  2. パラメータを渡すとエラーが表示されます。

ベクトル::消去()

消去() この関数は、コンテナーの指定された位置または範囲から要素を削除するために使用されます。

構文:

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

パラメーター:

  • イテレータの形式で削除される要素の位置。
  • 範囲は開始反復子と終了反復子を使用して指定されます。

結果: 要素はコンテナの指定された位置から削除されます。

例:

 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>

削除中からの要素 ある 特定の位置

例:

C++




// C++ program to demonstrate> // working of erase() function> #include> #include> using> namespace> std;> int> main()> {> >vector<>int>>myvector{ 1, 2, 3, 4, 5 };>> >vector<>int>>::それを反復処理します;>>' it = myvector.begin();> >myvector.erase(it);> >// Printing the Vector> >for> (>auto> it = myvector.begin(); it != myvector.end();> >++it)> >cout <<>' '> << *it;> >return> 0;> }>

>

>

出力

トポロジー
 2 3 4 5>

時間計算量: の上)
補助スペース: ○(1)

特定の要素を削除する

値に基づいて特定の要素を削除するには、まずその位置を知る必要があります。find() 関数を使用して見つけることができます。

例:

C++




// C++ program to remove element based on its value> #include> using> namespace> std;> int> main()> {> >vector<>int>>ベクトル = { 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;> }>

>

>

出力

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

範囲内の要素を削除する

C++




// C++ program to demonstrate> // Implementation of erase() function> #include> #include> using> namespace> std;> int> main()> {> >vector<>int>>myvector{ 1, 2, 3, 4, 5 };>> >vector<>int>>::イテレータ 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;> }>

>

>

出力

 4 5>

時間計算量: の上)
補助スペース: ○(1)

ベクトルペア要素の削除

例:

C++




#include> using> namespace> std;> // Function to print vector pair elements> void> print(vectorint, string>>&もの) { cout<< '['; for (int i = 0; i cout << '{' << vec[i].first << ',' << vec[i].second << '}'; if (i cout << ', '; } cout << ']' << endl; } int main() { vectorint, string>> x = { { 1, 'リンゴ' }, { 2, 'バナナ' }, { 3, 'チェリー' }, { 4, 'グアバ' } }; // 位置 1 (インデックス 0) の要素を削除します x.erase(x.begin()); プリント(x); // Print [{2,banana}, {3,cherry}, {4,Guava}] // 位置 0 と 1 の要素を削除します (インデックス 0 // 1) x.erase(x.begin(), x.begin() + 2); プリント(x); // Print [{4,Guava}] // ベクトルをクリア x.clear(); プリント(x); // 何も出力しません (空の括弧のみ) return 0; } // このコードは Susobhan Akhuli によって提供されました>>

>

>

出力

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

時間計算量: の上)
補助スペース: ○(1)

エラーと例外

  1. 位置が有効な場合、例外スローの保証はありません。
  2. それ以外の場合は未定義の動作を示します。

応用

整数のリストを指定して、ベクトルからすべての偶数要素を削除し、ベクトルを出力します。

入力:

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

出力:

1 3 5 7 9>

説明: 偶数でベクトルから消去された 2、4、6、8

アルゴリズム

  1. ベクトルのサイズまでループを実行します。
  2. 各位置の要素が 2 で割り切れるかどうかを確認し、割り切れる場合は要素を削除し、イテレータをデクリメントします。
  3. 最終的なベクトルを出力します。

以下のプログラムは上記のアプローチを実装しています。

C++




// C++ program to demonstrate> // Application of erase() function> #include> #include> using> namespace> std;> int> main()> {> >vector<>int>>myvector{ 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;> }>

>

>

出力

 1 3 5 7 9>

時間計算量: 消去には線形時間がかかるため、最悪の場合は O(N)。

clear() と Erase()、いつ何を使用するのですか?

クリア() ベクトル コンテナからすべての要素を削除し、そのサイズを 0 にします。ベクトルのすべての要素は、 クリア() 関数。

消去() 一方、関数は、コンテナから特定の要素、またはコンテナから要素の範囲を削除するために使用され、削除された要素の数だけサイズが削減されます。