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

简介: /* 关联式容器共性:二叉查找树实现,自动根据关键字排序,自动平衡 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());
	
}


 

结果:

 

相关文章
|
9月前
|
Linux Shell
linux自动崩溃,模拟测试
该脚本创建一个 systemd 服务和定时器,在系统启动3分钟后触发崩溃。通过向 /proc/sysrq-trigger 写入 &quot;c&quot; 来实现内核崩溃,用于测试系统崩溃后的恢复机制。
215 4
|
6月前
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
322 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
6月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
620 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
7月前
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
458 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
7月前
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
514 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
8月前
|
运维 Linux 开发者
Linux系统中使用Python的ping3库进行网络连通性测试
以上步骤展示了如何利用 Python 的 `ping3` 库来检测网络连通性,并且提供了基本错误处理方法以确保程序能够优雅地处理各种意外情形。通过简洁明快、易读易懂、实操性强等特点使得该方法非常适合开发者或系统管理员快速集成至自动化工具链之内进行日常运维任务之需求满足。
563 18
|
7月前
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
641 0
|
安全 Linux 测试技术
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
OpenText Static Application Security Testing (Fortify) 25.3 (macOS, Linux, Windows) - 静态应用安全测试
680 0
|
7月前
|
存储 安全 Linux
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
Kali Linux 2025.3 发布 (Vagrant & Nexmon) - 领先的渗透测试发行版
804 0
|
7月前
|
缓存 安全 Linux
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025082101 (Linux, Windows) - 专业渗透测试框架
286 0
下一篇
开通oss服务