C++ stl set用法例子
2016-04-13 20:11
441 查看
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; } |
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 32 second 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 300 myset contains: 1101 2202 |
相关文章推荐
- C++中rapidxml用法及例子
- C++ STL set集合容器常见用法
- C++ STL set 的基本用法
- 详解C++中string的用法和例子
- C++ vector详细用法(补充例子 持续更新)
- C++中rapidxml用法及例子(源码)
- C++ STL set集合容器常用用法
- C++ STL set集合容器常用用法
- C++中rapidxml用法及例子
- C++ STL set::find的用法
- C++ STL set::find的用法
- c++ string的用法和例子(官方手册)
- C++ STL set::find的用法
- C++ STL set_union的用法
- 一个例子演示了C++异常的推荐用法
- c++中的new操作符定位用法(placement new) 的一个例子
- C++中操作符operator的两种用法
- C++中,get getline gets getchar 用法
- C++ Vector用法
- c/c++中#号的用法