list::erase()是C++ STL中的內置函數,用於從列表容器中刪除元素。此函數可用於從指定的列表容器中刪除單個元素或一係列元素。
用法:
iterator list_name.erase(iterator position) or, iterator list_name.erase(iterator first, iterator last)
參數:此函數可以接受不同的參數,具體取決於它是用於從列表容器中刪除單個元素還是元素範圍。
- position:當該函數用於刪除單個元素時,使用此參數。此參數表示一個迭代器,該迭代器指向需要從列表容器中刪除的元素。
- first,last:當列表用於擦除範圍內的元素時,將使用這兩個參數。參數first指向該範圍內第一個元素的迭代器,參數last指向該範圍內需要刪除的最後一個元素的迭代器。這將擦除範圍中的所有元素,包括第一個由迭代器指向的元素,但排除最後一個由迭代器指向的元素。
返回值:此函數返回一個迭代器,該迭代器指向列表容器中的元素,該元素後麵是從列表容器中刪除的最後一個元素。
以下示例程序旨在說明list::erase()函數。
程序1::刪除單個元素。
// CPP program to illustrate the
// list::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a list
list<int> demoList;
// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
demoList.push_back(50);
// Printing elements of list before deleting
// first element
cout << "List before deleting first element: ";
for (auto itr = demoList.begin();
itr != demoList.end(); itr++) {
cout << *itr << " ";
}
// Creating iterator to point to first
// element in the list
list<int>::iterator itr = demoList.begin();
// deleting the first element
demoList.erase(itr);
// Printing elements of list after deleting
// first element
cout << "\nList after deleting first element:";
for (auto itr = demoList.begin();
itr != demoList.end(); itr++) {
cout << *itr << " ";
}
return 0;
}
輸出:
List before deleting first element: 10 20 30 40 50 List after deleting first element:20 30 40 50
程序2::刪除一係列元素。
// CPP program to illustrate the
// list::erase() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Creating a list
list<int> demoList;
// Add elements to the List
demoList.push_back(10);
demoList.push_back(20);
demoList.push_back(30);
demoList.push_back(40);
demoList.push_back(50);
// Printing elements of list before deleting
// any element
cout << "List before deleting any element: ";
for (auto itr = demoList.begin();
itr != demoList.end(); itr++) {
cout << *itr << " ";
}
// Creating iterators of the list
list<int>::iterator itr1, itr2;
itr1 = demoList.begin();
itr2 = demoList.begin();
// Incrementing itr2 by 3 positions
advance(itr2, 3);
// deleting range of elements from index [0, 3)
demoList.erase(itr1, itr2);
// Printing elements of list after deleting
// range of elements from [0, 3)
cout << "\nList after deleting first three elements: ";
for (auto itr = demoList.begin();
itr != demoList.end(); itr++) {
cout << *itr << " ";
}
return 0;
}
輸出:
List before deleting any element: 10 20 30 40 50 List after deleting first three elements: 40 50
注意:此函數以線性時間複雜度工作,即從列表容器中刪除的元素數。
相關用法
- C++ map erase()用法及代碼示例
- C++ unordered_set erase()用法及代碼示例
- C++ unordered_multiset erase()用法及代碼示例
- C++ set::erase用法及代碼示例
- C++ std::string::erase用法及代碼示例
- C++ multiset erase()用法及代碼示例
- C++ multimap::erase()用法及代碼示例
- C++ unordered_map erase用法及代碼示例
- C++ unordered_multimap erase用法及代碼示例
- C++ vector erase()、clear()用法及代碼示例
- C++ deque::clear()、deque::erase()用法及代碼示例
- C++ list end()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 list erase() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。