gpt4 book ai didi

c++ - 按字符串长度对 CMap 键进行排序

转载 作者:行者123 更新时间:2023-11-30 01:31:55 26 4
gpt4 key购买 nike

以前,我使用 STL 映射来执行上述任务。

struct ltstr
{
bool operator()(std::string s1, std::string s2) const
{
const int l1 = s1.length();
const int l2 = s2.length();
if (l1 == l2) {
// In alphabetical order.
return s1.compare(s2) < 0;
}
// From longest length to shortest length.
return l1 > l2;
}
};
std::map<std::string, int, ltstr> m;

如何使用 CMap 执行相同的任务?

// How to make key sorted by string length?
CMap<CString, LPCTSTR, int, int> m;

最佳答案

你不能。来自 the MSDN documentation for CMap :

You might think that this iteration is sequential by key value; it is not. The sequence of retrieved elements is indeterminate.

关于c++ - 按字符串长度对 CMap 键进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2467413/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com