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

std::map<key,value> 是根據key的"<"進行排序的,當使用struct做爲key需要重載“<”符號,用於map的排序和查找。

#include <string>
#include <map>
#include <iostream>

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 <mapKey,std::string> 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 <string>
#include <map>
#include <iostream>


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 <mapKey, std::string, mycmp> 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 <string>
#include <map>
#include <iostream>
#include <algorithm>

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 <mapKey,std::string> 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

完成根據特點參數查找。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章