1.知识点
2.源代码
#include <iostream>
#include <string>
#include <tchar.h>
#include <set>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "1.set/multiset对象的默认构造\n";
set<int> setInt;
cout << "2.set的插入与迭代器\n\n";
cout << "2.1set的默认排序:升序\n";
setInt.insert(3);
setInt.insert(1);
setInt.insert(5);
setInt.insert(2);
setInt.insert(2);
setInt.insert(2);
setInt.insert(2);
for (set<int>::iterator it = setInt.begin(); it != setInt.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "2.2set的降序排序:\n";
set<int, greater<int>> setInt2;
setInt2.insert(3);
setInt2.insert(1);
setInt2.insert(5);
setInt2.insert(2);
setInt2.insert(2);
setInt2.insert(2);
setInt2.insert(2);
for (set<int>::iterator it = setInt2.begin(); it != setInt2.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "3.函数对象functor的用法:\n";
class CStudent
{
public:
CStudent(int iID, string strName)
{
m_iID = iID;
m_strName = strName;
}
int GetiID() const { return m_iID; }
string GetStrName() const { return m_strName; }
private:
int m_iID;
string m_strName;
};
struct StuFunctor
{
bool operator()(const CStudent& stu1, const CStudent& stu2) const
{
return(stu1.GetiID() < stu2.GetiID());
}
};
set<CStudent, StuFunctor> setStu;
setStu.insert(CStudent(3, "小张"));
setStu.insert(CStudent(1, "小钱"));
setStu.insert(CStudent(5, "小王"));
setStu.insert(CStudent(2, "小刘"));
for (set<CStudent, StuFunctor>::iterator it = setStu.begin(); it != setStu.end(); ++it)
{
cout << it->GetiID() << " " << it->GetStrName() << endl;
}
cout << "\n\n";
cout << "4.set对象的拷贝构造与赋值\n";
set<int> setIntA;
setIntA.insert(3);
setIntA.insert(1);
setIntA.insert(5);
setIntA.insert(2);
set<int> setIntC;
setIntC.insert(30);
setIntC.insert(10);
setIntC.insert(50);
setIntC.insert(20);
cout << "遍历set对象setIntA\n";
for (set<int>::iterator it = setIntA.begin(); it != setIntA.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "遍历set对象setIntC\n";
for (set<int>::iterator it = setIntC.begin(); it != setIntC.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "4.1set对象的拷贝构造函数:set(const set &set);\n";
set<int> setIntB(setIntA);
for (set<int>::iterator it = setIntB.begin(); it != setIntB.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "4.2set对象的重载等号操作符:set& operator=(const set &st);\n";
set<int> setIntD;
setIntD = setIntC;
for (set<int>::iterator it = setIntD.begin(); it != setIntD.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "4.3交换两个集合容器:set.swap(st);\n";
setIntA.swap(setIntC);
cout << "遍历set对象setIntA\n";
for (set<int>::iterator it = setIntA.begin(); it != setIntA.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "遍历set对象setIntC\n";
for (set<int>::iterator it = setIntC.begin(); it != setIntC.end(); ++it)
{
int iItem = *it;
cout << iItem << " ";
}
cout << "\n\n";
cout << "5.set的大小 set.size(); set.empty();\n";
set<int> setIntE;
setIntE.insert(3);
setIntE.insert(1);
setIntE.insert(5);
setIntE.insert(2);
setIntE.insert(2);
setIntE.insert(2);
setIntE.insert(2);
if (setIntE.empty() == false)
{
cout << "setIntE的大小是:" << setIntE.size() << endl;
}
else
{
cout << "setIntE为空" << endl;
}
cout << "\n\n";
cout << "6.set的查找\n";
set<int> setIntF;
setIntF.insert(3);
setIntF.insert(1);
setIntF.insert(5);
setIntF.insert(2);
setIntF.insert(9);
setIntF.insert(8);
setIntF.insert(20);
cout << "6.1 set.find(elem);查找elem元素,返回指向elem元素的迭代器。\n";
set<int>::iterator it1 = setIntF.find(5);
cout << "setIntF中元素5的迭代器指向的值是:" << *it1 << endl << endl;
cout << "6.2 set.count(elem); \n";
int count1 = setIntF.count(3);
cout << "setIntF中元素5的个数是:" << count1 << endl << endl;
cout << "6.3 set.lower_bound(elem); 返回第一个 >= elem 元素的迭代器\n";
set<int>::iterator it2 = setIntF.lower_bound(9);
cout << "setIntF元素中第一个大于或等于9的元素的迭代器指向的值是:" << *it2 << endl << endl;
cout << "6.4 set.upper_bound(elem); 返回第一个>elem元素的迭代器。\n";
set<int>::iterator it3 = setIntF.upper_bound(9);
cout << "setIntF元素中第一个大于9的元素的迭代器指向的值是:" << *it3 << endl << endl;
cout << "6.5 set.equal_range(elem);返回容器中与elem相等的上下限的两个迭代器。\n";
pair<set<int>::iterator, set<int>::iterator>itPair = setIntF.equal_range(9);
set<int>::iterator itFirst = itPair.first;
set<int>::iterator itSecond = itPair.second;
cout << "set.equal_range(elem)返回的值:" << *itFirst << " " << *itSecond << endl << endl;
cout << "7.set的删除\n";
set<int> setIntG;
setIntG.insert(3);
setIntG.insert(1);
setIntG.insert(5);
setIntG.insert(2);
setIntG.insert(9);
setIntG.insert(8);
setIntG.insert(20);
cout << "遍历set对象setIntG,刚开始的时候\n";
for (set<int>::iterator it = setIntG.begin(); it != setIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.1 set.erase(elem); 删除容器中值为elem的元素。\n";
setIntG.erase(3);
cout << "遍历set对象setIntG,现在\n";
for (set<int>::iterator it = setIntG.begin(); it != setIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.2 set.erase(pos);删除pos迭代器所指的元素,返回下一个元素的迭代器。\n";
set<int>::iterator it_erase1 = ++setIntG.begin();
setIntG.erase(it_erase1);
cout << "遍历set对象setIntG,现在\n";
for (set<int>::iterator it = setIntG.begin(); it != setIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.3 set.clear();清除所有元素\n";
setIntG.clear();
cout << "遍历set对象setIntG,现在\n";
for (set<int>::iterator it = setIntG.begin(); it != setIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
return 0;
}
3.运行结果