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

标签 c++ mfc stl

以前,我使用 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/

相关文章:

c++ - 哪个是管理映射到其他一些小对象的小对象的正确容器?

c++ - OpenSSL:接受TLS连接,然后转移到另一个进程

visual-c++ - VS2008 : "Resource file opened in another editor"

c++ - 迭代器在与临时容器交换后变得无效

c++ - std::function 而不是谓词模板

c++ - Jetson TX2、Arduino Uno 串口通信

c++ - 如何将数据插入 std::map 并在 vc6 中显示来自 std::map 的数据

visual-c++ - 如何将 GetProfileBinary 的结果保存到智能指针中?

c++ - 打印用户定义类型的 std::vector

c++ - (c++, Armadillo ) 替换矩阵中的一部分列 vector