欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unordered_map

程序员文章站 2022-05-12 23:37:29
...

转载自: 

https://blog.csdn.net/hk2291976/article/details/51037095#332-insert

https://www.cnblogs.com/acbingo/p/4556427.html

1、基本操作:

#include<iostream>
#include<map>
#include<vector>
#include<unordered_map>
#include<string>
using namespace std;

void display(unordered_map<string, double> myrecipe, string str) {
	cout << str << endl;
	for (auto& x : myrecipe)
		cout << x.first << ":" << x.second << endl;
	cout << endl;
}
int main() {
	unordered_map<string, double> myrecipe, mypantry = { {"milk",2.0},{"flour",1.5} };
	pair<string, double> myshopping = { "baking powder",0.3 };
	/****************************插入********************************/
	//myrecipe.insert(pair<string,double("baking powder",0.3)>)
	myrecipe.insert(myshopping);//复制插入
	myrecipe.insert(make_pair<string, double>("eggs", 6.0));//移动插入
	myrecipe.insert(mypantry.begin(), mypantry.end());//范围插入
	myrecipe.insert({ {"suga",0.8},{"salt",0.1} });//初始化数组形势插入
	myrecipe["coffe"] = 1.0;//数组形式的插入

	display(myrecipe, "myrecipe contains:");
	/****************************查找********************************/
	unordered_map<string, double>::const_iterator got = myrecipe.find("coffe");
	if (got == myrecipe.end())
		cout << "not found" << endl;
	else
		cout << "Found " << got->first << " is " << got->second << endl;
	/****************************查找********************************/
	myrecipe.at("coffe") = 9.0;
	myrecipe["milk"] = 3.0;
	display(myrecipe, "After moddifing:");
	/****************************擦除********************************/
	myrecipe.erase(myrecipe.begin());
	myrecipe.erase("milk");
	display(myrecipe, "After erasing:");
	/****************************交换********************************/
	myrecipe.swap(mypantry);
	display(myrecipe, "After swapping:");
	/****************************清空********************************/
	myrecipe.clear();
	display(myrecipe, "After clearing:");
	while (1);
}

myrecipe contains:
baking powder:0.3
eggs:6
flour:1.5
milk:2
suga:0.8
salt:0.1
coffe:1

Found coffe is 1
After moddifing:
baking powder:0.3
eggs:6
flour:1.5
milk:3
suga:0.8
salt:0.1
coffe:9

After erasing:
eggs:6
flour:1.5
suga:0.8
salt:0.1
coffe:9

After swapping:
milk:2
flour:1.5

After clearing:

2、 make_pair

#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<unordered_map>
#include<string>
using namespace std;

bool cmp(const pair<int, string> a, const pair<int, string> b) {
	return a.first > b.first;//通过运算符重载,利用sort函数实现非升序排序
}
int main() {
	vector<pair<int, string>> vec;
	vec.push_back(make_pair<int, string>(5,"_bingo"));
	vec.push_back(make_pair<int, string>(4, "bin"));
	vec.push_back(make_pair<int, string>(6, "abc_bingo"));
	vec.push_back(make_pair<int, string>(6, "ABC_bingo"));
	vec[3].first++;
	cout << "Before sort:" << endl;
	for (auto element : vec)
		cout << element.first << ":" << element.second << endl;
	cout << endl;
	sort(vec.begin(), vec.end(),cmp);//sort默认是非降序排序,写入cmp函数,可以自定义排序方式
	for (auto element : vec)
		cout << element.first << ":" << element.second << endl;
	while (1);
}
Before sort:
5:_bingo
4:bin
6:abc_bingo
7:ABC_bingo

7:ABC_bingo
6:abc_bingo
5:_bingo
4:bin


 3、更多操作详见参考连接

 

更多hash函数相关知识:http://www.nowamagic.net/academy/detail/3008108