【Cherno的C++视频】Maps in C++(std::map and std::unordered_map)

#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>

// std::map and std::unordered_map.
// std::map is a tree, sorted. unordered_map is a hash table, usually faster than map.
// std::unordered_map: [] inserts elements if they're not there, no const version of this operator.
// use unordered_map whenever you can if you don't care about the order.
// iterating maps isn't the most ideal thing though.
// map generally performs better than unordered_map.

struct CityRecord
{
	std::string Name;
	uint64_t Population;
	double Latitude, Longitude;

	bool operator< (const CityRecord& other) const
	{
		return Population < other.Population;//sort by population.
	}
};

// creating a hash of CityRecord.
namespace std
{
	template<>
	struct hash<CityRecord>// put the custom type here.
	{
		size_t operator() (const CityRecord& key)//generates a unique hash for this specific object.
		{
			return hash<std::string>()(key.Name);//specify how that type(Name) should be hashed.
		}
	};
}

int main(void)
{
	//std::vector<CityRecord> citiesVec;
	//citiesVec.emplace_back("Melbourne", 5000000, 2.4, 9.4);
	//citiesVec.emplace_back("Lol-town", 5000000, 2.4, 9.4);
	//citiesVec.emplace_back("Berlin", 5000000, 2.4, 9.4);
	//citiesVec.emplace_back("Paris", 5000000, 2.4, 9.4);
	//citiesVec.emplace_back("London", 5000000, 2.4, 9.4);
	 using a vector is not ideal for searching one element.
	//for (const auto& city : citiesVec)
	//{
	//	if (city.Name == "Berlin")
	//	{
	//		city.Population;
	//		break;
	//	}
	//}

	 using map: the larger your data set is the faster this will essentially be.(such as 1 million elements)
	//std::map<std::string, CityRecord> cityMap;
	//cityMap["Melbourne"] = CityRecord{"Melbourne", 5000000, 2.4, 9.4};
	//cityMap["town"] = CityRecord{"Lol-town", 5000000, 2.4, 9.4};
	//cityMap["Berlin"] = CityRecord{"Berlin", 5000000, 2.4, 9.4};
	//cityMap["Paris"] = CityRecord{"Paris", 5000000, 2.4, 9.4};
	//cityMap["London"] = CityRecord{"London", 5000000, 2.4, 9.4};
	//CityRecord& berlinMapData = cityMap["Berlin"];
	//berlinMapData.Population;

	// using unordered_map.
	std::map<std::string, CityRecord> cityMap;
	cityMap["Melbourne"] = CityRecord{ "Melbourne", 5000000, 2.4, 9.4 };
	cityMap["Lol-town"] = CityRecord{ "Lol-town", 98571, 2.4, 9.4 };
	cityMap["Berlin"] = CityRecord{ "Berlin", 5000000, 2.4, 9.4 };
	cityMap["Paris"] = CityRecord{ "Paris", 42523, 2.4, 9.4 };
	cityMap["London"] = CityRecord{ "London", 5129879, 2.4, 9.4 };

	std::map<CityRecord, uint32_t> cityFounded;
	cityFounded[CityRecord{ "Melbourne", 5000000, 2.4, 9.4 }];//[] will insert the element in.

	// remove an element.
	cityMap.erase("Paris");

	const auto& newData = cityMap;
	if (newData.find("Berlin") != newData.end())
	{
		const CityRecord& berlinData = newData.at("Berlin");//.at():it'll never insert new elements but return the elements to you in a const state.
	}

	// iterate through a unordered_map:cityMap.
	for (auto& [name, city] : cityMap)
	{
		std::cout << name << "\n Population: " << city.Population << std::endl;
		cityFounded[city];//just insert a key here.
	}
	std::cout << "=======================" << std::endl;
	// iterate through cityFounded.
	for (auto& [city, founded] : cityFounded)
	{
		std::cout << city.Name << "\n Population: " << city.Population << std::endl;
	}

	std::cin.get();
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值