栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ map multimao operator

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ map multimao operator

创建
#include 
#include //所有元素都是pair
//第一个元素是key(索引)
//第二个元素是value(实值)
//map不可重复,multimap可以重复
#include 
#include 
using namespace std;
int main() {
	map  stu;
	srand(time(0));
	cout << "按照key排序" << endl;
	stu.insert(pair (1, rand() % 20 + 1));
	stu.insert(pair (3, rand() % 20 + 1));
	stu.insert(pair (2, rand() % 20 + 1));
	stu.insert(pair (4, rand() % 20 + 1));
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout << "拷贝构造" << endl;
	map  stu1(stu);
	for (auto a : stu1) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout << "赋值" << endl;
	map  stu2=stu;
	for (auto a : stu2) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
}

大小和交换
#include 
#include //所有元素都是pair
//第一个元素是key(索引)
//第二个元素是value(实值)
//map不可重复,multimap可以重复
#include 
#include 
using namespace std;
int main() {
	map  stu;
	map  stu1;
	srand(time(0));
	cout << "按照key排序" << endl;
	stu.empty() ? cout << "为空" << endl : cout << "不为空" << endl;
	cout << "大小是:" << stu.size() << endl;
	stu.insert(pair (1, rand() % 20 + 1));
	stu.insert(pair (3, rand() % 20 + 1));
	stu.insert(pair (2, rand() % 20 + 1));
	stu.insert(pair (4, rand() % 20 + 1));
	stu.empty() ? cout << "为空"<(5, rand() % 20 + 1));
	stu1.insert(pair (6, rand() % 20 + 1));
	stu1.insert(pair (7, rand() % 20 + 1));
	stu1.insert(pair (8, rand() % 20 + 1));
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout << endl;
	for (auto a : stu1) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout <
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout << endl;
	for (auto a : stu1) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}

}

插入和删除
#include 
#include //所有元素都是pair
//第一个元素是key(索引)
//第二个元素是value(实值)
//map不可重复,multimap可以重复
#include 
#include 
using namespace std;
int main() {
	map  stu;
	map  stu1;
	srand(time(0));
	cout << "按照key排序" << endl;
	stu.insert(pair (1, rand() % 20 + 1));
	stu.insert(make_pair(3, rand() % 20 + 1));
	stu[2]= rand() % 20 + 1;//不建议
	stu.insert(pair (4, rand() % 20 + 1));
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	cout << endl << stu[5] << endl;
	stu.erase(1);
	cout << endl << "删除key值1" << endl;
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	stu.clear();
	cout << "为空" << endl;
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}

}

插入和查找
#include 
#include //所有元素都是pair
//第一个元素是key(索引)
//第二个元素是value(实值)
//map不可重复,multimap可以重复
#include 
#include 
using namespace std;
int main() {
	map  stu;
	map  stu1;
	srand(time(0));
	cout << "按照key排序" << endl;
	stu.insert(pair (1, rand() % 20 + 1));
	stu.insert(make_pair(3, rand() % 20 + 1));
	stu[2]= rand() % 20 + 1;//不建议
	stu.insert(pair (4, rand() % 20 + 1));
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
	map ::iterator pos;
	pos=stu.find(1);
	if (pos != stu.end())
		cout << "存在这个元素,位置是->"
		<<(*pos).first
		<< ",元素值是->" << (*pos).second;
	else
		cout << "不存在这个元素";
	cout << endl;
	int num = stu.count(3);
	cout << "3" << "的key为->" << num << endl;
	num = stu.count(5);
	cout << "5" << "的key为->" << num << endl;
	multimap aa;
	aa.insert(make_pair(1, 1));
	aa.insert(make_pair(1, 2));
	aa.insert(make_pair(2, 3));
	 num = aa.count(1);
	cout << "1" << "的key为->" << num << endl;

}

仿函数
#include 
#include //所有元素都是pair
//第一个元素是key(索引)
//第二个元素是value(实值)
//map不可重复,multimap可以重复
#include 
#include 
using namespace std;
class paixu {
public:

	bool operator()(int b1, int b2)const {
		return b1 > b2;
	}

};
int main() {
	map  stu;
	cout << "仿函数operator" << endl;
	cout << "改变排序规则(创建时就要改变)" << endl;
	srand(time(0));
	cout << "按照key排序" << endl;
	stu.insert(make_pair(1, rand() % 20 + 1));
	stu.insert(make_pair(3, rand() % 20 + 1));
	stu[2]= rand() % 20 + 1;//不建议
	stu.insert(make_pair(4, rand() % 20 + 1));
	for (auto a : stu) {
		cout << "编号:" << a.first << ",年龄:" << a.second << endl;
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966286.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号