#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
void myprint(int val)
{
cout << val << " " ;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(1);
s1.insert(40);
s1.insert(50);
s1.insert(60);
for_each(s1.begin(), s1.end(), myprint);
set<int>s2(s1);//拷贝构造
cout << endl;
for_each(s2.begin(), s2.end(), myprint);
}
int main()
{
test01();
system("pause");
return 0;
}
//find(key)查找key是否存在,如存在,就返回该建元素的迭代器,如果不存在就返回set.end
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
void myprint(int val)
{
cout << val << " " ;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(1);
s1.insert(20);
s1.insert(50);
s1.insert(60);
set<int>::iterator pos=s1.find(20);
if (pos != s1.end())
{
cout << "zhaodao" << *pos << endl;
}
else
cout << "find none" << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
count查找key的个数对于key而言要么是0,要么是1
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
void myprint(int val)
{
cout << val << " " ;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(1);
s1.insert(20);
s1.insert(20);
s1.insert(60);
int num=s1.count(20);
cout << num;
}
int main()
{
test01();
system("pause");
return 0;
}
//low_bound()返回第一个key>=keyelement元素的迭代器,up_bound()返回第一个key>keyelement元素的迭代器
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
void myprint(int val)
{
cout << val << " " ;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(20);
s1.insert(1);
s1.insert(20);
s1.insert(20);
s1.insert(60);
set<int>::iterator pos = s1.lower_bound(55);
cout << *pos;
}
int main()
{
test01();
system("pause");
return 0;
}
qual_range();
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
void myprint(int val)
{
cout << val << " " ;
}
void test01()
{
set<int>s1;
s1.insert(1);
s1.insert(20);
s1.insert(1);
s1.insert(15);
s1.insert(20);
s1.insert(60);
pair<set<int>::iterator, set<int>::iterator> pos = s1.equal_range(59);
if (pos.first != s1.end())
{
cout << *(pos.first) << endl;
}
else
cout << "find none";
if (pos.second!= s1.end())
{
cout << *(pos.second) << endl;
}
else
cout << "find none";
}
int main()
{
test01();
system("pause");
return 0;
}
pair的用法
#include<iostream>
#include<string>
#include<set>
using namespace std;
void test01()
{
pair<string, int>p("cui",10);
cout << p.first << " " << p.second << endl;
pair<string, int>p1=make_pair("yang",12);
cout << p1.first << " " << p1.second << endl;
}
int main()
{
test01();
system("pause");
}
//set容器里排序,不能再数据插入后在排序,这样会破坏他的结构,必须在插入前定义好他的类型和仿函数
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
class mycompare
{
public:
bool operator()(int val,int val1)
{
if (val>val1)
return true;
else
{
return false;
}
}
};
void test01()
{
set<int,mycompare>s1;
s1.insert(1);
s1.insert(20);
s1.insert(1);
s1.insert(15);
s1.insert(20);
s1.insert(60);
for (set<int, mycompare>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
}
int main()
{
test01();
system("pause");
return 0;
}
//set的自定义数据类型
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
class person
{
public:
person(string mName, int nNum) :mName(mName), nNum(nNum){};
string mName;
int nNum;
};
class mycompare
{
public:
bool operator()(const person &p1, const person &p2)
{
if (p1.nNum>p2.nNum)
return true;
else
{
return false;
}
}
};
void test01()
{
set<person,mycompare>s1;
person p1("cui", 12);
person p2("yang", 16);
person p3("wang", 13);
person p4("nan", 14);
s1.insert(p1);
s1.insert(p2);
s1.insert(p3);
s1.insert(p4);
for (set<person, mycompare>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout <<"姓名:"<<it->mName << " "<<"学号:"<<it->nNum<<endl
;
}
}
int main()
{
test01();
system("pause");
return 0;
}