如何更改地图容器';s的内部分类方案? 我是初学者C++程序员,所以我不理解语言结构,这妨碍了我理解地图API(参考文献)

如何更改地图容器';s的内部分类方案? 我是初学者C++程序员,所以我不理解语言结构,这妨碍了我理解地图API(参考文献),c++,dictionary,containers,C++,Dictionary,Containers,更重要的是,一些问题: 如何更改map的内部排序方案,以便在使用map::时,键值按字母顺序排序 更具体地说,关于map::key_comp,如果我们定义相同类型的两个元素“不相等(一个小于另一个)”意味着什么,那么排序是在内部自动完成的,所以我们只需要插入键/值对就可以了,这是一个定义并忘记的事情吗?或者我们必须定义相等/排序,然后显式调用函数以返回布尔值以实现有序插入吗?下面是一个示例,说明如何为排序映射提供模板参数以使用非默认排序: std::map<int, int, std::g

更重要的是,一些问题:

如何更改map的内部排序方案,以便在使用
map::
时,键值按字母顺序排序


更具体地说,关于
map::key_comp
,如果我们定义相同类型的两个元素“不相等(一个小于另一个)”意味着什么,那么排序是在内部自动完成的,所以我们只需要插入键/值对就可以了,这是一个定义并忘记的事情吗?或者我们必须定义相等/排序,然后显式调用函数以返回布尔值以实现有序插入吗?

下面是一个示例,说明如何为排序映射提供模板参数以使用非默认排序:

std::map<int, int, std::greater<int> > m;
std::map m;
取自


另外,对于一个更复杂的示例:

下面是一个示例,说明如何为排序映射提供一个模板参数以使用非默认排序:

std::map<int, int, std::greater<int> > m;
std::map m;
取自


还有一个更复杂的例子:

[Solved]我选择的解决方案

假设您的地图是:
map myMap

然后,您需要做两件事来定义“compAB”:

定义一个返回true或false/(或1或0)的比较函数。我还没有测试过这个函数是否可以接受多个参数(尽管我不明白为什么它不能,只要它返回一个
bool
。这个函数应该指定如何在映射中对与
值相同类型的两个对象进行排序

bool compare(int a, int b)
{
    if(a < b) return true; 
    return false; //Default
}
然后,您可以进行声明:
map 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; 
}

其他示例。

[Solved]我选择的解决方案

假设您的地图是:
map myMap;

然后,您需要做两件事来定义“compAB”:

定义一个返回true或false/(或1或0)的比较函数。我还没有测试这个函数是否可以接受多个参数(虽然我不明白为什么它不能,只要它返回一个
bool
。此函数应该指定如何对与地图中的
值相同类型的两个对象进行排序

bool compare(int a, int b)
{
    if(a < b) return true; 
    return false; //Default
}
然后,您可以进行声明:
map 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; 
}

其他示例。

请将您的解决方案作为答案发布,不要将其编辑到您的问题中。请将您的解决方案作为答案发布,不要将其编辑到您的问题中。