STL 映射和集合中的排序顺序
用户定义的对象在map和set中是如何排序的? 据我所知,映射/集合是排序关联容器:插入的元素根据其保存的键进行排序。
但map和set内部使用operator >
来对它们的元素进行排序。
从 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;
}
在上面的示例中,值是如何排序的?
编辑:代码从注释中移出:
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 are the user defined objects sorted in map and 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.
But map and set internally use operator >
to sort their elements.
From the SGI site, I have the following examples:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
std::map
使用函子对元素进行排序。默认情况下是std::less
使用运算符<
。在您的示例中,有一个用户定义的函子ltstr
,它将有助于根据元素的键按字母顺序对元素进行排序。std::map
uses a functor to sort elements. By default is itstd::less<Key>
which usesoperator<
. In your sample there is an user defined functorltstr
which will help to sort elements according to its keys in alphabetical order.首先,默认情况下使用
operator<
,而不是operator>
。在您的情况下,您在创建地图对象时通过传递第三个模板参数来传递自定义比较函数。在将每个元素插入到映射时,此比较函子用于确定映射中对象的相对顺序,即用于比较键。例如,当您执行months["february"] = 28;
时,map 会比较键“january”和“february”。由于我们正在进行字符串比较,因此该比较返回一个大于 0 的值。该值用于确定键“february”相对于“january”的位置。First of all
operator<
is used by default and notoperator>
. 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 i.e. it is used to compare the keys. For example, when you domonths["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".