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

}

相关推荐
阿正的梦工坊几秒前
【Rust】03-所有权、移动与复制
开发语言·算法·rust
yi念zhi间3 分钟前
C#实现控制台多区域输出
开发语言·c#
阿坤带你走近大数据4 分钟前
分别介绍下java主流的开发框架、设计模式与对应编程语言的高级特性
java·开发语言·设计模式
小小龙学IT6 分钟前
Go 后端开发中的并发模式:从 Goroutine 到 Pipeline 实战
开发语言·后端·golang
小短腿的代码世界7 分钟前
Qt文本布局引擎深度解析:从QTextDocument排版到渲染的完整架构
开发语言·qt·架构
Leweslyh9 分钟前
《3GPP TS 28.312 面向移动网络的意图驱动管理服务》完整自学教程
开发语言·网络·php
2501_9307077811 分钟前
使用 C# 在 Excel 中合并并居中单元格
开发语言·c#·excel
好评笔记13 分钟前
深度学习面试八股—— GRU(Gated Recurrent Unit)
人工智能·rnn·深度学习·算法·机器学习·gru·校招
哎呦,帅小伙哦13 分钟前
一个通用的异步任务提交器
c++
aidou131413 分钟前
Kotlin中自定义RadioGroup实现多个RadioButton自动换行
android·开发语言·kotlin·shape·radiobutton·selector·radiogroup