c++ set容器

#include <iostream>

#include <string>

#include <fstream>

#include <vector>

#include <algorithm>

#include <deque>

#include <stack>

#include <queue>

#include <list>

#include <set>

using namespace std;

void printSet(const set<int> &s)

{

for (set<int>::const_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(50);

s1.insert(20);

s1.insert(40);

s1.erase(s1.begin());

s1.erase(50);

printSet(s1);

int num=s1.count(30);

cout << "num = " << num << endl;

if(s1.empty())

{

cout << "s1 is empty" << endl;

}

else

{

cout << "s1 is not empty, size = " << s1.size() << endl;

}

}

void test02()

{

set<int> s1;

pair<set<int>::iterator, bool> ret = s1.insert(10);

if(ret.second)

{

cout << "insert " << *(ret.first) << " success" << endl;

}

else

{

cout << "insert " << *(ret.first) << " failed" << endl;

}

ret = s1.insert(10);

if(ret.second)

{

cout << "insert " << *(ret.first) << " success" << endl;

}

else

{

cout << "insert " << *(ret.first) << " failed" << 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;

}

void test03()

{

pair<string,int> p("hello",1);

cout << p.first << " " << p.second << endl;

pair<string,int> p2(p);

cout << p2.first << " " << p2.second << endl;

}

class MyCompare

{

public:

bool operator()(int v1,int v2)const

{

return v1>v2;

}

};

void test05()

{

set<int,MyCompare> s5;

s5.insert(10);

s5.insert(20);

s5.insert(30);

for(set<int,MyCompare>::iterator it = s5.begin(); it != s5.end(); it++)

{

cout << *it << " ";

}

}

class Person

{

public:

Person(string name,int age)

{

this->m_Name = name;

this->m_Age = age;

}

string m_Name;

int m_Age;

};

class PersonCompare

{

public:

bool operator()(const Person &p1, const Person &p2)const

{

return p1.m_Age > p2.m_Age;

}

};

void test06()

{

set<Person,PersonCompare> s1;

Person p1("aaa",10);

Person p2("bbb",20);

Person p3("ccc",30);

s1.insert(p1);

s1.insert(p2);

s1.insert(p3);

for(set<Person>::iterator it = s1.begin(); it != s1.end(); it++)

{

cout << "name = " << it->m_Name << " age = " << it->m_Age << endl;

}

}

int main()

{

//test01();

//test02();

//test03();

//test05();

test06();

return 0;

system("pause");

}

相关推荐
橘颂TA10 分钟前
【Linux 网络】TCP 拥塞控制与异常处理:从原理到实践的深度剖析
linux·运维·网络·tcp/ip·算法·职场和发展·结构与算法
星辰徐哥18 分钟前
Rust函数与流程控制——构建逻辑清晰的系统级程序
开发语言·后端·rust
liliangcsdn19 分钟前
如何使用lambda对python列表进行排序
开发语言·python
tobias.b1 小时前
408真题解析-2010-9-数据结构-折半查找的比较次数
java·数据结构·算法·计算机考研·408真题解析
源代码•宸1 小时前
Leetcode—404. 左叶子之和【简单】
经验分享·后端·算法·leetcode·职场和发展·golang·dfs
WBluuue1 小时前
数据结构与算法:dp优化——优化尝试和状态设计
c++·算法·leetcode·动态规划
java 乐山1 小时前
c 写一个文本浏览器(1)
c语言·开发语言
im_AMBER1 小时前
Leetcode 105 K 个一组翻转链表
数据结构·学习·算法·leetcode·链表
sin_hielo1 小时前
leetcode 1877
数据结构·算法·leetcode
windows_61 小时前
MISRA C:2025 规则逐条分析
c语言·开发语言