如何更改地图容器的内部排序方案?
How do I change the map container's internal sorting scheme?
我是一名初学者 C++ 程序员,所以有些语言结构我不理解,这使我无法理解地图的 API(供您参考,here)
言归正传,一些问题:
如何更改地图的内部排序方案,以便在我们使用 map::<string, ...>
的情况下,键值按字母顺序排序?
更具体地说,关于 map::key_comp
,它是否是一个定义后忘记的事情,一旦我们定义了相同类型的两个元素是 "unequal (one is less than the other)" 的含义,那么排序就是在内部自动完成 - 所以我们只需要插入 key/value 对?或者我们是否必须定义 equality/ordering 然后显式调用函数为 return 一个布尔值来实现有序插入?
下面是一个示例,说明如何为排序映射提供模板参数以使用非默认排序:
std::map<int, int, std::greater<int> > m;
取自C++ std::map items in descending order of keys
另外,举个更复杂的例子:how to declare custom sort function on std::map declaration?
[已解决]我选择的解决方案
假设您的地图是:map<int,vector<int>,compAB> myMap;
然后你需要做两件事来定义"compAB":
定义一个return真或假/(或1或0)的比较函数。我还没有测试过这个函数是否可以接受多个参数(虽然我不明白为什么它不能只要它 returns a bool
。这个函数应该指定你打算如何订购两个与地图中 key
值相同类型的对象。
bool compare(int a, int b)
{
if(a < b) return true;
return false; //Default
}
map::key_comp 对象的 API (here 表示如果第一个参数是 "less than"/[ 则比较函数需要 return true =44=] 第二个参数。它应该 return false 否则来源)。在这里,我使用了一个简单的标准来确定 "less than":如果 a
创建一个结构,其成员仅包含一个运算符重载:
struct compAB
{
bool operator()(int i1, int i2){
return compare(i1, i2);
}
};
然后,您可以声明:map<int,vector<int>,compAB> myMap;
并且对 insertion() 的任何调用都会根据您指定的键排序方案插入您指定的对
例如:
#include <iostream>
#include <map>
bool compare_descend(int a, int b)
{
//specifies that when a>b return true -> a FOLLOWS b
if(b < a) return true;
return false;
}
bool compare_ascend(int a, int b)
{
//specifies that when a<b return true -> a PRECEDES b
if(a < b) return true;
return false; //Default
}
struct comp_asc
{
bool operator()(int i1, int i2)
{
return compare_ascend(i1, i2);
}
};
int main()
{
std::cout << "int_map_asc: [key,value]\n";
//Map declaration via class function
std::map<int,std::string,comp_asc> int_map_asc;
//Insert pairs into the map
int_map_asc.insert( std::pair<int,std::string>(0, "Alan") );
int_map_asc.insert( std::pair<int,std::string>(1, "Baye") );
int_map_asc.insert( std::pair<int,std::string>(2, "Carl") );
int_map_asc.insert( std::pair<int,std::string>(3, "David") );
//Iterate & print
std::map<int,std::string,comp_asc>::iterator a_it;
for(a_it=int_map_asc.begin(); a_it!=int_map_asc.end(); a_it++)
std::cout << "[" << a_it->first << "," << a_it->second << "]\n";
std::cout << "\nint_map_desc: [key,value]\n";
//Map declaration via function pointer as compare
bool(*fn_compare_desc)(int,int) = compare_descend; //Create function ptr to compare_descend()
std::map<int,std::string,bool(*)(int,int)> int_map_desc(fn_compare_desc); //fn ptr passed to constructor
//Insert pairs into the map
int_map_desc.insert( std::pair<int,std::string>(0, "Alan") );
int_map_desc.insert( std::pair<int,std::string>(1, "Baye") );
int_map_desc.insert( std::pair<int,std::string>(2, "Carl") );
int_map_desc.insert( std::pair<int,std::string>(3, "David") );
//Ititerate & print
std::map<int,std::string,bool(*)(int,int)>::iterator d_it;
for(d_it=int_map_desc.begin(); d_it!=int_map_desc.end(); d_it++)
std::cout << "[" << d_it->first << "," << d_it->second << "]\n";
return 0;
}
输出:
int_map_asc: [key,value]
[0,Alan]
[1,Baye]
[2,Carl]
[3,David]
int_map_desc: [key,value]
[3,David]
[2,Carl]
[1,Baye]
[0,Alan]
其他示例 here。
我是一名初学者 C++ 程序员,所以有些语言结构我不理解,这使我无法理解地图的 API(供您参考,here)
言归正传,一些问题:
如何更改地图的内部排序方案,以便在我们使用 map::<string, ...>
的情况下,键值按字母顺序排序?
更具体地说,关于 map::key_comp
,它是否是一个定义后忘记的事情,一旦我们定义了相同类型的两个元素是 "unequal (one is less than the other)" 的含义,那么排序就是在内部自动完成 - 所以我们只需要插入 key/value 对?或者我们是否必须定义 equality/ordering 然后显式调用函数为 return 一个布尔值来实现有序插入?
下面是一个示例,说明如何为排序映射提供模板参数以使用非默认排序:
std::map<int, int, std::greater<int> > m;
取自C++ std::map items in descending order of keys
另外,举个更复杂的例子:how to declare custom sort function on std::map declaration?
[已解决]我选择的解决方案
假设您的地图是:map<int,vector<int>,compAB> myMap;
然后你需要做两件事来定义"compAB":
定义一个return真或假/(或1或0)的比较函数。我还没有测试过这个函数是否可以接受多个参数(虽然我不明白为什么它不能只要它 returns a bool
。这个函数应该指定你打算如何订购两个与地图中 key
值相同类型的对象。
bool compare(int a, int b)
{
if(a < b) return true;
return false; //Default
}
map::key_comp 对象的 API (here 表示如果第一个参数是 "less than"/[ 则比较函数需要 return true =44=] 第二个参数。它应该 return false 否则来源)。在这里,我使用了一个简单的标准来确定 "less than":如果 a
创建一个结构,其成员仅包含一个运算符重载:
struct compAB
{
bool operator()(int i1, int i2){
return compare(i1, i2);
}
};
然后,您可以声明:map<int,vector<int>,compAB> myMap;
并且对 insertion() 的任何调用都会根据您指定的键排序方案插入您指定的对
例如:
#include <iostream>
#include <map>
bool compare_descend(int a, int b)
{
//specifies that when a>b return true -> a FOLLOWS b
if(b < a) return true;
return false;
}
bool compare_ascend(int a, int b)
{
//specifies that when a<b return true -> a PRECEDES b
if(a < b) return true;
return false; //Default
}
struct comp_asc
{
bool operator()(int i1, int i2)
{
return compare_ascend(i1, i2);
}
};
int main()
{
std::cout << "int_map_asc: [key,value]\n";
//Map declaration via class function
std::map<int,std::string,comp_asc> int_map_asc;
//Insert pairs into the map
int_map_asc.insert( std::pair<int,std::string>(0, "Alan") );
int_map_asc.insert( std::pair<int,std::string>(1, "Baye") );
int_map_asc.insert( std::pair<int,std::string>(2, "Carl") );
int_map_asc.insert( std::pair<int,std::string>(3, "David") );
//Iterate & print
std::map<int,std::string,comp_asc>::iterator a_it;
for(a_it=int_map_asc.begin(); a_it!=int_map_asc.end(); a_it++)
std::cout << "[" << a_it->first << "," << a_it->second << "]\n";
std::cout << "\nint_map_desc: [key,value]\n";
//Map declaration via function pointer as compare
bool(*fn_compare_desc)(int,int) = compare_descend; //Create function ptr to compare_descend()
std::map<int,std::string,bool(*)(int,int)> int_map_desc(fn_compare_desc); //fn ptr passed to constructor
//Insert pairs into the map
int_map_desc.insert( std::pair<int,std::string>(0, "Alan") );
int_map_desc.insert( std::pair<int,std::string>(1, "Baye") );
int_map_desc.insert( std::pair<int,std::string>(2, "Carl") );
int_map_desc.insert( std::pair<int,std::string>(3, "David") );
//Ititerate & print
std::map<int,std::string,bool(*)(int,int)>::iterator d_it;
for(d_it=int_map_desc.begin(); d_it!=int_map_desc.end(); d_it++)
std::cout << "[" << d_it->first << "," << d_it->second << "]\n";
return 0;
}
输出:
int_map_asc: [key,value]
[0,Alan]
[1,Baye]
[2,Carl]
[3,David]
int_map_desc: [key,value]
[3,David]
[2,Carl]
[1,Baye]
[0,Alan]
其他示例 here。