c++ stl erase
C ++ STL set :: erase()函数 (C++ STL set::erase() function)
set::erase() function is a predefined function, it is used to erase an element from a set.
set :: erase()函数是预定义的函数,用于删除集合中的元素。
Prototype:
原型:
set<T> st; //declaration
set<T>::iterator it; //iterator declaration
st.erase( const T item); //prototype 1
or
st.erase(iterator position) //prototype 2
Parameter:
参数:
const T item; //prototype 1
Or
Iterator position //prototype 2
Return type:
返回类型:
size_type //prototype 1
Or
void //prototype 2
Usage: The function is used to erase an element based on its value or iterator position from the set
用法:该函数用于根据元素的值或元素在集合中的迭代器位置来擦除它
Example:
例:
For a set of integer,
set<int> st;
set<int>::iterator it;
st.insert(4);
st.insert(5);
set content:
4
5
st.erase(4);
set content:
5
st.erase(st.begin()); //erases 5
set content:
empty set
Header file to be included:
包含的头文件:
#include <iostream>
#include <set>
OR
#include <bits/stdc++.h>
C++ implementation:
C ++实现:
#include <bits/stdc++.h>
using namespace std;
void printSet(set<int> st){
set<int>:: iterator it;
cout<<"Set contents are:\n";
for(it=st.begin();it!=st.end();it++)
cout<<*it<<" ";
cout<<endl;
}
int main(){
cout<<"Example of erase function\n";
set<int> st;
set<int>:: iterator it;
cout<<"inserting 4\n";
st.insert(4);
cout<<"inserting 6\n";
st.insert(6);
cout<<"inserting 10\n";
st.insert(10);
printSet(st); //printing current set
cout<<"erasing 6..\n";
st.erase(6); //prototype 1
cout<<"After erasing 6...\n";
printSet(st);
cout<<"erasing first element of the set now\n";
st.erase(st.begin());//prototype 2
cout<<"after erasing first element of set now\n";
printSet(st);
return 0;
}
Output
输出量
Example of erase function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10
erasing 6..
After erasing 6...
Set contents are:
4 10
erasing first element of the set now
after erasing first element of set now
Set contents are:
10
翻译自: https://www.includehelp.com/stl/set-erase-function-in-cpp-stl.aspx
c++ stl erase