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");

}

相关推荐
Navigator_Z10 分钟前
LeetCode //C - 1286. Iterator for Combination
c语言·算法·leetcode
SEO_juper24 分钟前
用 Python 写一个 GEO 可见性检查脚本:你的网站现在能被 AI 引用吗
开发语言·人工智能·爬虫·python·seo·外贸独立站
西柚小萌新40 分钟前
【LLM&&AI应用开发 八股文】--4.3.Agent智能体(下)
java·开发语言·数据库
北极有牛42 分钟前
cpp学习笔记--常量指针
java·开发语言·算法
GPU实战笔记42 分钟前
云端 GPU 训练的账户余额提醒:它能说明什么,不能说明什么?
深度学习·算法·成本管理·gpu云计算·云端训练
半杯咖啡半行码1 小时前
C++ 编程与 STL 模板:从泛型编程到内存安全详解
开发语言·c++
浮链序1 小时前
怎么证明"你这个模型是偷我的"——把蒸馏变成取证工具
算法·安全·llm
小O的算法实验室1 小时前
IEEE TMC,面向异构物联网的多目标量子行为拓扑优化算法
算法
caoerzhong1 小时前
JeeWMS 开源 WMS 部署避坑指南:Java 仓库管理系统的环境基线、四类根因与可复现交付
java·开发语言·开源
时间的拾荒人1 小时前
Qt 界面优化:绘图详解 —— 从基础图形到图片处理
开发语言·qt·面试