stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...
生活随笔
收集整理的這篇文章主要介紹了
stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
stl list 刪除元素
list.remove()和list.remove_if()函數(shù) (list.remove() and list.remove_if() functions)
remove() function is used to remove all occurrences of a given element from the list and function remove_if() is used to remove set of some specific elements from the list.
remove()函數(shù)用于從列表中刪除所有出現(xiàn)的給定元素,而remove_if()函數(shù)用于從列表中刪除某些特定元素的集合。
Example:
例:
List elements are11223344551122Element to remove: 11List element after removing 112233445522Condition to remove some specific elements: all ODD numbersList element after removing all ODD numbers224422Program:
程序:
#include <iostream> #include <list> using namespace std;int main() {//declaring a list list<int> iList = {11, 22, 33, 44, 55, 11, 22};//declaring iterator to the listlist<int>::iterator l_iter;//printing list elementscout<<"List elements are"<<endl;for (l_iter = iList.begin(); l_iter != iList.end(); l_iter++)cout<< *l_iter<<endl;//remove 11 from the ListiList.remove(11);cout<<"List elements after removing 11"<<endl;for (l_iter = iList.begin(); l_iter != iList.end(); l_iter++)cout<< *l_iter<<endl;//remove all ODD numbersiList.remove_if([](int n){return (n%2!=0); });cout<<"List elements after removing all ODD numbers"<<endl;for (l_iter = iList.begin(); l_iter != iList.end(); l_iter++)cout<< *l_iter<<endl;return 0; }Output
輸出量
List elements are 11 22 33 44 55 11 22 List elements after removing 11 22 33 44 55 22 List elements after removing all ODD numbers 22 44 22翻譯自: https://www.includehelp.com/stl/remove-all-occurrences-of-an-element-and-remove-set-of-some-specific-from-the-list.aspx
stl list 刪除元素
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的stl list 删除元素_删除所有出现的元素,并从列表中删除一些特定的元素。 C ++ STL...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++中std::find_std ::
- 下一篇: 术中导航_密码术中的计数器(CTR)模式