如何使用特定值從 C++ STL 容器中移除項?
Erase 函式可用於從 C++ STL 容器中刪除特定值。
演算法
Begin Declare vector v and iterator it to the vector. Initialize the vector. Erase() function is used to remove item from end. Print the remaining elements. End.
示例程式碼
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v{ 6,7,8,9,10};
vector<int>::iterator it;
it = v.end();
it--;
v.erase(it);
for (auto it = v.begin(); it != v.end(); ++it)
cout << ' ' << *it;
return 0;
}輸出
The current content of the vector is : 6 7 8 9 10 Please enter the element to be deleted -> 7 The current content of the vector is : 6 8 9 10
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
python
C 程式設計
C++
C#
MongoDB
MySQL
javascript
PHP