C++ stl set用法例子
来源:互联网 发布:黛堡嘉莱价格淘宝 编辑:程序博客网 时间:2024/06/02 00:37
EG:
#include<stdio.h>
#include<string>
#include<set>
#include<iostream>
usingnamespace std;
intmain(){
int a[]={1,23,45,215,22,11,2,5,78,23};
int j;
set<int>::iterator it;
set<int> myset(a,a+10);
scanf("%d",&j);
myset.insert(j);
for(it=myset.begin();it!=myset.end();it++){
cout<<*it<<" ";
}
getchar();getchar();getchar();
return0;
}
四个功能如下:
insert 插入元素。
erase 删除元素。
swap 替换元素。
clear 删除所有元素。
myset(数组名).insert(你要使用的功能名)(j(你要进行操作的变量));
Example
// erasing from set#include <iostream>#include <set>using namespace std;int main (){ set<int> myset; set<int>::iterator it; // insert some values: for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 it=myset.begin(); it++; // "it" points now to 20 myset.erase (it); myset.erase (40); it=myset.find (60); myset.erase ( it, myset.end() ); cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) cout << " " << *it; cout << endl; return 0;}Output:
myset contains: 10 30 50
Example
// swap sets#include <iostream>#include <set>using namespace std;main (){ int myints[]={12,75,10,32,20,25}; set<int> first (myints,myints+3); // 10,12,75 set<int> second (myints+3,myints+6); // 20,25,32 set<int>::iterator it; first.swap(second); cout << "first contains:"; for (it=first.begin(); it!=first.end(); it++) cout << " " << *it; cout << "\nsecond contains:"; for (it=second.begin(); it!=second.end(); it++) cout << " " << *it; cout << endl; return 0;}
Output:
first contains: 20 25 32second contains: 10 12 75
Example
// set::clear#include <iostream>#include <set>using namespace std;int main (){ set<int> myset; set<int>::iterator it; myset.insert (100); myset.insert (200); myset.insert (300); cout << "myset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) cout << " " << *it; myset.clear(); myset.insert (1101); myset.insert (2202); cout << "\nmyset contains:"; for (it=myset.begin(); it!=myset.end(); ++it) cout << " " << *it; cout << endl; return 0;}
Output:
myset contains: 100 200 300myset contains: 1101 2202
1 0
- C++ stl set用法例子
- [C++::STL]之set的用法
- STL::set用法
- STL------set用法!!!
- STL-set用法
- STL中set用法
- STL用法 (set)
- 【转】STL-set用法
- STL set的用法
- STL set基本用法
- STL 【set用法】
- STL-set-用法
- stl set的用法
- set的用法 STL
- STL set用法详解
- STL set 用法
- 【C++ STL】Set用法
- 【STL】set用法
- 第一个swift程序
- 关于QEMU中的类型算法
- Emacs中文识别以及中文输入法的相关问题
- UVA 10635 Prince and Princess lcs--》lis
- c#中关于结构体和字节数组转化
- C++ stl set用法例子
- 将el表达式list遍历的的数值,传递到jquery的click函数里面,实现删除
- Android开发工具的下载
- 九宫格dfs
- c++ STL学习笔记
- hdu 1058 Humble Numbers
- Redis参数配置说明
- C++反射的实现
- Python smtplib发送邮件 包含文本、附件、图片等