std::map的使用和解释
- 重载operator的时候,都要声明public
- 自定义key值,用class MyDefineKey的结构作为自定义的key值,可以正常使用find去查找
3. 原因:二叉红黑树结构,有序集合,在插入数据的时候,要重载操作operator函数,否则会编译报错
5. 函数重载: 传入的参数是比较的右值,本身的成员变量是右值 - map声明的第三个参数
4. 传入的参数必须是【函数指针】或者一个【结构类型】
5. 函数指针:
6. 使用c++11的新函数decltype(),该函数分析传入的参数,并分析它的类型,不具体计算它的实际值
1-decltype(func) -----返回的是type的类型
2-decltype(&func)-----函数地址指针(满足第三个参数)
3-decltype(func)*-----函数指针(满足第三个参数)
7. 声明函数指针传入
1-typedef (*func)(type, type) FUNC;
2- type (*func)(type, type) = NULL; func=Myfunc;
3-std::map<key_type, type, func_type> Map(Myfunc);
6. 结构或类
7.声明一个类型struct/class,并重载操作函数bool operate()(type, type)const - map insert 的返回值,std::pair<iterator, bool>包含了两个参数,一个是map 的迭代器,另一个是bool值
- 插入相同键的数据,map会插入失败,保留原来的数值,不会更新对应的数值
源码
template < class Key, // map::key_type
class T, // map::mapped_type
class Compare = less<Key>, // map::key_compare
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
> class map;
自定义key重载函数
bool operator ==(const Type &)const{}
bool operator <(const Type &)const{}
测试代码
#include <iostream>
#include <map>
using namespace std;
//1-测试 unordered_map 用make_pair作为唯一key插入
// 2-测试 map 是否可以用find查找
class MyDefineKey
{
public:
MyDefineKey(const int KeyOne, const int KeyTwo):m_KeyOne(KeyOne), m_KeyTwo(KeyTwo)
{
}
bool operator ==(const MyDefineKey &val)const
{
if(val.m_KeyOne == m_KeyOne && val.m_KeyTwo == m_KeyTwo) return true;
return false;
}
bool operator <(const MyDefineKey &val)const
{
if(val.m_KeyOne > m_KeyOne && val.m_KeyTwo > m_KeyTwo) return true;
return false;
}
int m_KeyOne;
int m_KeyTwo;
};
//map的第三个参数----->自定义函数传入
bool DefineCompare( const MyDefineKey &left, const MyDefineKey &right)
{
if(left.m_KeyOne > right.m_KeyOne) return true;
return false;
}
//map的第三个参数----->自定义类型参入 class 或者struct都可以
class Com
{
public:
bool operator()(const MyDefineKey& left, const MyDefineKey & right)const
{
if(left.m_KeyTwo < right.m_KeyTwo) return true;
return false;
}
};
map:1-自定义key 2-find 3-insert 4-insert的返回值:std::pair<iterator, bool>
void DefineKey()
{
std::map<MyDefineKey, int> MyDefineMap;
typedef std::map<MyDefineKey, int>::iterator MyMapIt;
for(int i = 1; i <= 4; ++i)
{
MyDefineKey key1(i-1,i);
MyDefineMap[key1] = i;
}
cout << "Test for MyDefineKey" << endl;
for(std::map<MyDefineKey, int>::iterator it = MyDefineMap.begin(); it != MyDefineMap.end(); ++it)
{
cout << "Key : ( " << it->first.m_KeyOne << "," << it->first.m_KeyTwo << ") value:" << it->second <<endl;
}
}
void TestFind()
{
MyDefineKey findKey(1,2);
std::map<MyDefineKey, int>::iterator it = MyDefineMap.find(findKey);
if(it != MyDefineMap.end())
{
cout << "Got it" << "Key : ( " << it->first.m_KeyOne << "," << it->first.m_KeyTwo << ") value:" << it->second <<endl;
}
else
{
cout << "Nothing" << endl;
}
}
void TestMake_Pair()
{
cout << "Test for make_pair or pair" << endl;
MyDefineKey PairKey(100,200);
int value = 200;
MyDefineMap.insert(std::make_pair(PairKey, value));
// MyDefineMap.insert(std::pair<MyDefineKey, int>(PairKey, value));
cout << "Test for same value insert" << endl;
std::pair<MyMapIt, bool> ref = MyDefineMap.insert(std::make_pair(PairKey, 900));
if(ref.second)
{
cout << "Insert success" << endl;
}
else
{
cout << "Insert fail" << endl;
}
for(std::map<MyDefineKey, int>::iterator it = MyDefineMap.begin(); it != MyDefineMap.end(); ++it)
{
cout << "Key : ( " << it->first.m_KeyOne << "," << it->first.m_KeyTwo << ") value:" << it->second <<endl;
}
}
//map的第三个参数的使用:1-自定义排序 2-函数指针类型
void TestThirdParam()
{
cout << "compare function point Test" << endl;
bool (*func)(const MyDefineKey&, const MyDefineKey&) = DefineCompare;
std::map<MyDefineKey, int, bool(*)(const MyDefineKey&, const MyDefineKey&) > MyDefineMap2 (func);
for(int i = 1; i <= 4; ++i)
{
MyDefineKey key2(i-1,i);
MyDefineMap2[key2] = i;
}
for(std::map<MyDefineKey, int>::iterator it = MyDefineMap2.begin(); it != MyDefineMap2.end(); ++it)
{
cout << "Key : ( " << it->first.m_KeyOne << "," << it->first.m_KeyTwo << ") value:" << it->second <<endl;
}
cout << "com struct Test" << endl;
std::map<MyDefineKey, int, Com> MyDefineMap3;
for(int i = 1; i <= 4; ++i)
{
MyDefineKey key3(i-1,i);
MyDefineMap3[key3] = i;
}
for(std::map<MyDefineKey, int>::iterator it = MyDefineMap3.begin(); it != MyDefineMap3.end(); ++it)
{
cout << "Key : ( " << it->first.m_KeyOne << "," << it->first.m_KeyTwo << ") value:" << it->second <<endl;
}
}
int main()
{
/测试map
// 1-自定义key值
DefineKey();
// 2-自定义key值重载operator可直接使用find函数
TestFind();
// 3-make_pair可以键值直接插入数据
TestMake_Pair();
// 4-map的第三个参数
TestThirdParam();
}
输出结果