C++ STL set::erase() 函数
set::erase() 函数是一个预定义的函数,用于从集合中擦除一个元素。
原型:
set<T> st; //declaration set<T>::iterator it; //iterator declaration st.erase( const T item); //prototype 1 or st.erase(iterator position) //prototype 2
参数:
const T item; //prototype 1 Or Iterator position //prototype 2
返回类型:
size_type //prototype 1 Or void //prototype 2
用法:该函数用于根据元素的值或迭代器位置从集合中擦除元素
例:
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
要包含的头文件:
#include <iostream> #include <set> OR #include <bits/stdc++.h>
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;
}
输出
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
相关用法
- C++ set::erase用法及代码示例
- C++ set::empty()用法及代码示例
- C++ set::emplace()用法及代码示例
- C++ set::rbegin()、set::rend()用法及代码示例
- C++ set::begin()、set::end()用法及代码示例
- C++ set::lower_bound()用法及代码示例
- C++ set::size()用法及代码示例
- C++ set::clear用法及代码示例
- C++ set::find()用法及代码示例
- C++ set::insert()用法及代码示例
- C++ set::swap()用法及代码示例
- C++ set::upper_bound()用法及代码示例
- C++ set::clear()用法及代码示例
- C++ set::key_comp()用法及代码示例
- C++ set rbegin()用法及代码示例
- C++ set upper_bound()用法及代码示例
- C++ set swap()用法及代码示例
- C++ set size()用法及代码示例
- C++ set lower_bound()用法及代码示例
- C++ set erase()用法及代码示例
注:本文由纯净天空筛选整理自 set::erase() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。