简体   繁体   English

在STL地图中排序顺序并设置

[英]Sort Order in STL map and set

How are the user defined objects sorted in map and set? 用户定义的对象如何在map和set中排序? As far as I know, map/set are Sorted Associative Containers: the elements being inserted are sorted based on the key that it holds. 据我所知,map / set是Sorted Associative Containers:插入的元素是根据它所拥有的键进行排序的。

But map and set internally use operator > to sort their elements. 但map和set内部使用operator >来对它们的元素进行排序。

From the SGI site, I have the following examples: 从SGI站点,我有以下示例:

struct ltstr
{
    bool operator()(const char* s1, const char* s2) const
    {
        return strcmp(s1, s2) < 0;
    }
};

int main()
{
    map<const char*, int, ltstr> months;

    months["january"] = 31;
    months["february"] = 28;
    months["march"] = 31;
    months["april"] = 30;
    months["may"] = 31;
    months["june"] = 30;
    months["july"] = 31;
    months["august"] = 31;
    months["september"] = 30;
    months["october"] = 31;
    months["november"] = 30;
    months["december"] = 31;

    cout << "june -> " << months["june"] << endl;

    map<const char*, int, ltstr>::iterator cur  = months.find("june");
    map<const char*, int, ltstr>::iterator prev = cur;
    map<const char*, int, ltstr>::iterator next = cur;

    ++next;
    --prev;

    cout << "Previous (in alphabetical order) is " << (*prev).first << endl;
    cout << "Next (in alphabetical order) is " << (*next).first << endl;
}

In the above example, how are the values sorted? 在上面的例子中,值是如何排序的?

Edit: Code moved from comment: 编辑:代码从评论移开:

typedef map <string, int> Mint ;

int main() 
{
    string Name ;
    int Marks;
    Mint Grade;
    for (int i = 0; i<4; i++) 
    {
        cin>> Name ; 
        cin >> Marks; 
        Grade [Name] = Marks ; 
    } 
    Mint :: iterator iter; 
    for( iter = Grade.begin(); iter != Grade.end(); iter++)
        cout<< (*iter).first<<“ \t ” <<(*iter).second<<“\n” ; 
    return 0; 

} 

How would the values get sorted? 如何对值进行排序?

std::map uses a functor to sort elements. std::map使用仿函数对元素进行排序。 By default is it std::less<Key> which uses operator< . 默认情况下是使用operator< std::less<Key> In your sample there is an user defined functor ltstr which will help to sort elements according to its keys in alphabetical order. 在您的示例中,有一个用户定义的函子ltstr ,它将帮助按字母顺序按键对元素进行排序。

First of all operator< is used by default and not operator> . 首先,默认使用operator<而不是operator> In your case, you are passing a custom comparison function by passing the third template parameter while creating the map object. 在您的情况下,您通过在创建地图对象时传递第三个模板参数来传递自定义比较函数。 While inserting each element to the map, this comparison functor is used to determine the relative ordering of the object in the map ie it is used to compare the keys. 在将每个元素插入到地图时,此比较函数用于确定对象在地图中的相对顺序,即用于比较键。 For example, when you do months["february"] = 28; 例如,当你做months["february"] = 28; , map compares the the keys "january" and "february". ,地图比较了“一月”和“二月”的关键。 Since we are doing a string compare, this comparison returns a value greater than 0. This value is used to determine the position of the key "february" in relation to "january". 由于我们正在进行字符串比较,因此该比较返回大于0的值。该值用于确定键“二月”相对于“一月”的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM