[C++STL]set容器用法介绍
Posted Wecccccccc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[C++STL]set容器用法介绍相关的知识,希望对你有一定的参考价值。
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
set<int>s2(s1);
printSet(s2);
set<int>s3;
s3 = s2;
printSet(s3);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
if (s1.empty())
{
cout << "s1 empty" << endl;
}
else
{
cout << "s1 no empty" << endl;
cout << "s1 size = " << s1.size() << endl;
}
set<int>s2;
s2.insert(100);
s2.insert(300);
s2.insert(200);
s2.insert(400);
cout << "交换前" << endl;
printSet(s1);
printSet(s2);
cout << "交换后" << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
printSet(s1);
s1.erase(s1.begin());
printSet(s1);
s1.erase(30);
printSet(s1);
s1.clear();
printSet(s1);
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
插入 — insert
删除 — erase
清空 — clear
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "find elem" << *pos<<endl;
}
else
{
cout << "no find elem" << endl;
}
int num = s1.count(30);
cout << "num = " << num << endl;
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s;
pair<set<int>::iterator, bool> ret = s.insert(10);
if (ret.second)
{
cout << "success 1" << endl;
}
else
{
cout << "fail 1" << endl;
}
ret = s.insert(10);
if (ret.second)
{
cout << "success 2" << endl;
}
else
{
cout << "fail 2" << endl;
}
multiset<int>ms;
ms.insert(10);
ms.insert(10);
for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
set存放内置数据类型
代码如下:
#include <iostream>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
class cmp
{
public:
bool operator()(int a, int b)
{
return a > b;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(40);
s1.insert(20);
s1.insert(30);
s1.insert(50);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
set<int, cmp>s2;
s2.insert(10);
s2.insert(40);
s2.insert(20);
s2.insert(30);
s2.insert(50);
for (set<int, cmp>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
利用仿函数可以指定set容器的排序规则。
set存放自定义数据类型
代码如下:
#include <iostream>
#include <string>
#include <set>
using namespace std;
void printSet(set<int>&s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
class Person
{
public:
Person(string name, int age):myName(name),myAge(age){}
string myName;
int myAge;
};
class cmp
{
public:
bool operator()(const Person & a, const Person &b)
{
return a.myAge > b.myAge;
}
};
void test01()
{
set<Person, cmp> s;
typedef set<Person, cmp> setpc;
Person p1("Tom", 23);
Person p2("Mike", 27);
Person p3("Bom", 25);
Person p4("Jack", 21);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (setpc::iterator it = s.begin(); it != s.end(); it++)
{
cout << "name = " << it->myName << "age = " << it->myAge << endl;
}
}
int main()
{
test01();
return 0;
}
测试结果:
总结:
对于自定义数据类型,set必须指定排序规则才可以插入数据。
以上是关于[C++STL]set容器用法介绍的主要内容,如果未能解决你的问题,请参考以下文章