C++set容器-插入和删除
生活随笔
收集整理的這篇文章主要介紹了
C++set容器-插入和删除
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
set插入和刪除
功能描述:
set容器進(jìn)行插入數(shù)據(jù)和刪除數(shù)據(jù)
函數(shù)原型:
代碼如下:
#include <iostream> using namespace std; #include <set>void printSet(set<int > &s) {for (set<int >::iterator it = s.begin(); it != s.end(); it++) {cout << *it << " ";}cout << endl; }//set容器 插入和刪除 void test01() {set<int >s1;//插入s1.insert(30);s1.insert(10);s1.insert(20);s1.insert(40);//遍歷printSet(s1);//刪除第一個(gè)元素 注意:set容器會(huì)自動(dòng)排序,故刪除的一個(gè)元素為10s1.erase(s1.begin());printSet(s1);//刪除重載版本s1.erase(30);printSet(s1);//清空//s1.erase(s1.begin(),s1.end());s1.clear();printSet(s1);}int main() {test01();return 0; }總結(jié)
以上是生活随笔為你收集整理的C++set容器-插入和删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++set容器-大小和交换
- 下一篇: C++set容器-查找和统计