linux下练习 c++ 关联式容器共性测试,使用

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: /* 关联式容器共性:二叉查找树实现,自动根据关键字排序,自动平衡 set,multiset,map,multimap 查找:.find(key) 失败返回.
/*
关联式容器共性:二叉查找树实现,自动根据关键字排序,自动平衡
		  set<K>,multiset<K>,map<K,V>,multimap<K,V>
查找:.find(key) 失败返回.end()
统计:.count(key)
删除:.erase(key)
插入:.insert(element)
区间:.lower_bund(key) //取得关键字为key的第一个元素位置
	 .upper_bound(key) //取得关键字为key的最后一个元素之后的位置
	 .equal_range(key) 取得关键字为key的区间,返回pair
构造函数可用比较函数作为参数  bool func(K a,K b)
*/
#include<iostream>
#include<set>
#include<string>
using namespace std;
#include "print.h"
struct person
{
	string name;
	int age;
public:
	person(const char* n,int a):name(n),age(a){}
};
bool operator<(const person& a,const person& b)
{
	return a.age<b.age||(a.age==b.age&& a.name<b.name);//找的时候按这个找
}
ostream& operator<<(ostream& o,const person& x)
{
	return o<<x.name<<':'<<x.age<<"  ";
}
int main()
{
	multiset<person> mp;
	mp.insert(person("ccc",16));
	mp.insert(person("aaa",13));
	mp.insert(person("aaa",13));
	mp.insert(person("kkk",18));
	mp.insert(person("fff",15));
	mp.insert(person("eee",11));
	mp.insert(person("jjj",16));
	print(mp.begin(),mp.end());
	multiset<person>::iterator it=mp.find(person("fff",15));
	if(it==mp.end()) cout<<"not find!\n";
	else
	{
	 cout<<"find:"<<*it
         <<" "<<mp.count(*it)<<"个\n";
	}
	person a("aaa",13);
	cout<<a<<" "<<mp.count(a)<<"个\n";
	cout<<"lower/upper bound方法:\n";
	multiset<person>::iterator ibegin,iend;
	ibegin=mp.lower_bound(a);
	iend=mp.upper_bound(a);
	print(ibegin,iend);
	cout<<"pair方法:\n";
	typedef multiset<person>::iterator myiter;//给长类型起个别名
	pair<myiter,myiter> p=mp.equal_range(a);
	print(p.first,p.second);
	cout<<"删除后输出:\n";
	mp.erase(person("kkk",18));//有多个就删除多个
	print(mp.begin(),mp.end());
	
}


 

结果:

 

相关文章
|
6天前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
20 6
|
22天前
|
Linux 编译器 程序员
【Linux 调试秘籍】深入探索 C++:运行时获取堆栈信息和源代码行数的终极指南
【Linux 调试秘籍】深入探索 C++:运行时获取堆栈信息和源代码行数的终极指南
65 0
|
26天前
|
网络协议 Shell Linux
【Shell 命令集合 网络通讯 】⭐⭐⭐Linux 测试与目标主机之间的网络连接ping 命令 使用指南
【Shell 命令集合 网络通讯 】⭐⭐⭐Linux 测试与目标主机之间的网络连接ping 命令 使用指南
41 1
|
24天前
|
消息中间件 Linux 调度
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
【Linux 进程/线程状态 】深入理解Linux C++中的进程/线程状态:阻塞,休眠,僵死
63 0
|
22天前
|
监控 Linux 编译器
Linux C++ 定时器任务接口深度解析: 从理论到实践
Linux C++ 定时器任务接口深度解析: 从理论到实践
63 2
|
22天前
|
存储 Linux 程序员
【Linux C/C++ 堆内存分布】深入理解Linux进程的堆空间管理
【Linux C/C++ 堆内存分布】深入理解Linux进程的堆空间管理
67 0
|
22天前
|
存储 算法 Linux
深入理解Linux内存管理brk 和 sbrk 与以及使用C++ list实现内存分配器
深入理解Linux内存管理brk 和 sbrk 与以及使用C++ list实现内存分配器
31 0
|
22天前
|
存储 监控 Linux
Linux 使用getrusage系统调用获取cpu信息:一个C++实例分析
Linux 使用getrusage系统调用获取cpu信息:一个C++实例分析
48 0
|
23天前
|
Unix Linux C++
【C/C++ 造轮子】Linux异步计时器:深入探讨和应用 (Linux Asynchronous Timers: An In-depth Exploration and Application)
【C/C++ 造轮子】Linux异步计时器:深入探讨和应用 (Linux Asynchronous Timers: An In-depth Exploration and Application)
52 1
|
24天前
|
缓存 Linux iOS开发
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
61 1