企业官网网站模板下载,wordpress 作品展,莱芜雪野湖酒店,wordpress 账号 登陆不了set只有一个方法就是insert #includeiostream
#includeset
//set和multiset是一个头文件
//set内部实现机制 红黑色#xff08;平衡二叉树的一种#xff09;
//关联式容器
//set不允许有重复元素
//multiset运行有重复元素
//容器查找效率高
//容器根据元素的…
set只有一个方法就是insert #includeiostream
#includeset
//set和multiset是一个头文件
//set内部实现机制 红黑色平衡二叉树的一种
//关联式容器
//set不允许有重复元素
//multiset运行有重复元素
//容器查找效率高
//容器根据元素的值自动对元素排序 默认从小到大
using namespace std;//仿函数
class mycompare {
public:bool operator()(int v1, int v2) {return v1 v2;}
};
void PrintSet(setint s1) {for (setint::iterator it s1.begin(); it ! s1.end(); it){cout *it ;}cout endl;
}//set初始化
void test01() {mycompare com;com(10, 20); //仿函数setint s1;s1.insert(5);s1.insert(2);s1.insert(8);s1.insert(1);for (setint::iterator it s1.begin(); it ! s1.end(); it){cout *it ;}//拷贝构造setint s2(s1);PrintSet(s2);//赋值setint s3;s3 s1;//删除操作s1.erase(s1.begin());cout 删除s1.begin() endl;PrintSet(s1);s1.erase(8);cout 删除8 endl;PrintSet(s1);//如何改变默认排序//先序遍历 中序遍历 后序遍历
}//set查找
void test02() {//实值setint s1;s1.insert(5);s1.insert(2);s1.insert(8);s1.insert(1);PrintSet(s1);setint::iterator ret s1.find(8);cout find 8 endl;if (ret s1.end()) {cout 没有找到 endl;}else {cout ret:*ret endl;}//lower_bound找第一个大于等于指定值的元素 返回为迭代器cout lower_bound(大于等于) 4 endl;setint::iterator ret1 s1.lower_bound(4); if (ret1 s1.end()) {cout 没有找到 endl;}else {cout ret1: *ret1 endl;}//upper_bound找第一个大于指定值的元素 返回为迭代器cout upper_bound(大于) 2 endl;setint::iterator ret2 s1.upper_bound(2);if (ret2 s1.end()) {cout 没有找到 endl;}else {cout ret2: *ret2 endl;}//equal_range 返回lower_bound 和 upper_bound值/*setint::iterator e1;setint::iterator e2;paire1, e2s1.equal_range(5); 错误做法*/cout equal_range返回lower_bound 和 upper_bound值 5 endl;pairsetint::iterator, setint::iterator myret s1.equal_range(5);myret.first;myret.second;if (myret.first s1.end()) {cout 没有找到 endl;}else {cout 找到了 *myret.first endl;}if (myret.second s1.end()) {cout 没有找到 endl;}else {cout 找到了 *myret.second endl;}}class Person {
public:int id;int age;
public:Person(int Age,int Id):age(Age),id(Id){}
};
class mycompare2 {
public:bool operator() (Person p1, Person p2) const{return p1.age p2.age; //用什么排序 把什么作为key关键字}
};
void test03() {//setPerson sp;//Person p1(20, 10), p2(30, 30), p3(34, 12);//sp.insert(p1);//sp.insert(p2);//sp.insert(p3); //无法对p1,p2,p3进行排序 所以运行报错setPerson,mycompare2 sp;Person p1(20, 10), p2(30, 30), p3(34, 12);Person p4(20, 10);sp.insert(p1);sp.insert(p2);sp.insert(p3); //无法对p1,p2,p3进行排序 所以运行报错for (setPerson, mycompare2::iterator it sp.begin(); it ! sp.end(); it) {cout (*it).age (*it).id endl;}setPerson, mycompare2::iterator ret sp.find(p4);if (ret sp.end()) {cout 未找到 endl;}else {cout Age:(*ret).age endl;}
}
int main(int) {cout test01 endl;test01();cout test02 endl;test02();cout test03 endl;test03();return 0;
} #includeiostream
#includestring
using namespace std;//类模板的实现需要指定类型
//函数模板的实现不需要指定类型
//类模板templateclass T1,class T2 struct pair;void test01() {//第一种方法创建一个对组 构造 方法pairstring, int pair1(string(name), 1);cout pair1.first endl;cout pair1.second endl;//第二种pairstring, int pair2 make_pair(name, 30);cout pair2.first endl;cout pair2.second endl;//pair 赋值pairstring, int pair3 pair2;cout pair3.first endl;cout pair3.second endl;
}int main() {test01();return 0;
}