std::map 自定义key的排列和查找

std::map 是根据key的"<"进行排序的,当使用struct做为key需要重载“<”符号,用于map的排序和查找。

#include 
#include 
#include 

typedef struct mapKey_s
{
	int ikey;
	std::string skey;
}mapKey;



bool operator<(const mapKey& lhs, const mapKey& rhs)
{
	//用sKey作为主要的排列
	if (lhs.skey != rhs.skey)
	{
		return lhs.skey < rhs.skey;
	}
	else if (lhs.ikey != rhs.ikey)
	{
		return lhs.ikey < rhs.ikey;
	}
	return false;
}

typedef std::map  mapTest;

int _tmain(int argc, _TCHAR* argv[])
{
	mapTest maptest;
	int input;
	maptest[{10, "aaaa"}] = "dddd";
	maptest[{9, "bbbb"}] = "cccc";
	maptest[{6, "eeee"}] = "eeee";
	maptest[{8, "cccc"}] = "bbbb";
	maptest[{7, "dddd"}] = "aaaa";

	for each (auto tmp in maptest)
	{
		std::cout << tmp.first.skey << " " << tmp.first.ikey << " " << tmp.second << std::endl;
	}
	std::cin >> input;
	return 0;
}

输出

aaaa 10 dddd
bbbb 9 cccc
cccc 8 bbbb
dddd 7 aaaa
eeee 6 eeee

自动根据skey排列。

除了重载“<”也可以使用std::map的第三参数进行排列

#include "stdafx.h"
#include 
#include 
#include 


typedef struct mapKey_s
{
	int ikey;
	std::string skey;
}mapKey;

typedef struct mycmp_s {
	bool operator()(const mapKey &lhs, const mapKey &rhs)
	{
		//用sKey作为主要的排列
		if (lhs.skey != rhs.skey)
		{
			return lhs.skey < rhs.skey;
		}
		else if (lhs.ikey != rhs.ikey)
		{
			return lhs.ikey < rhs.ikey;
		}
		return false;
		
	}
}mycmp;

typedef std::map  mapTest2;

int _tmain(int argc, _TCHAR* argv[])
{
	mapTest2 maptest;
	int input;
	maptest[{10, "aaaa"}] = "dddd";
	maptest[{9, "bbbb"}] = "cccc";
	maptest[{6, "eeee"}] = "eeee";
	maptest[{8, "cccc"}] = "bbbb";
	maptest[{7, "dddd"}] = "aaaa";

	for each (auto tmp in maptest)
	{
		std::cout << tmp.first.skey << " " << tmp.first.ikey << " " << tmp.second << std::endl;
	}
	std::cin >> input;
	return 0;
}

输出

aaaa 10 dddd
bbbb 9 cccc
cccc 8 bbbb
dddd 7 aaaa
eeee 6 eeee

自动根据skey排列。

 

std::map的find是判断!(lhs < rhs) && !(rhs < lhs),使用上面的方法可以使用:

auto it = maptest.find({ 6, "eeee" });

查找对应key的iterator,但是要根据key或者value中的某一项来自定义查找规则就要使用find_if

#include "stdafx.h"
#include 
#include 
#include 
#include 

typedef struct mapKey_s
{
	int ikey;
	std::string skey;
}mapKey;



bool operator<(const mapKey& lhs, const mapKey& rhs)
{
	//用sKey作为主要的排列
	if (lhs.skey != rhs.skey)
	{
		return lhs.skey < rhs.skey;
	}
	else if (lhs.ikey != rhs.ikey)
	{
		return lhs.ikey < rhs.ikey;
	}
	return false;
}

typedef std::map  mapTest;

class map_finder
{
public:
	map_finder(const std::string &cmp_string) :m_findKey(cmp_string){}
	bool operator ()(const mapTest::value_type &pair)
	{
		return pair.first.skey == m_findKey;
	}
private:
	const std::string &m_findKey;
};

int _tmain(int argc, _TCHAR* argv[])
{
	mapTest maptest;
	int input;
	maptest[{10, "aaaa"}] = "dddd";
	maptest[{9, "bbbb"}] = "cccc";
	maptest[{6, "eeee"}] = "eeee";
	maptest[{8, "cccc"}] = "bbbb";
	maptest[{7, "dddd"}] = "aaaa";

	auto it = std::find_if(maptest.begin(), maptest.end(), map_finder("eeee"));
	if (it != maptest.end())
	{
		std::cout << it->first.skey << " " << it->first.ikey << " " << it->second << std::endl;
	}

	
	std::cin >> input;
	return 0;
}

输出
eeee 6 eeee

完成根据特点参数查找。

你可能感兴趣的:(std)