【问题标题】:How to access elements from a CMap?如何从 CMap 访问元素?
【发布时间】:2016-07-18 05:11:19
【问题描述】:

有人可以告诉我如何根据特定键获取特定元素。说如果我有一个 CMap ButtonProp; 现在我想访问提供的任何 int 值的属性,该怎么做? 附言第一次使用 CMap。 “ButtonProp.Lookup(int)”够用吗?

【问题讨论】:

    标签: c++ visual-studio-2015 mfc


    【解决方案1】:

    你基本上有两种选择:

    如果您知道该元素存在,则可以使用CMap::operator[]

    否则,您应该使用CMap::Lookup,但用法与您的问题不同。

    假设你从

    CMap<int,int,CPoint,CPoint> myMap;
    
    myMap.InitHashTable(257);
    

    要找到对应于 3 的元素,您可以使用

    myMap[3] 
    

    CPoint ret;
    bool found = myMap.Lookup(3, ret);
    

    如果foundtrue,那么ret 就是您的答案。

    【讨论】:

      【解决方案2】:

      下面是简单的代码sn-p,演示CMap容器的用法:

      CMap<CString, LPCTSTR, int, int> NameToValueMap;
      
      CString sKey(_T("Test"));
      int nValue = 10;        
      NameToValueMap[sKey] = nValue;
      
      
      if (NameToValueMap.IsEmpty())
          return;
      
      int nVal;
      if (NameToValueMap.Lookup(sKey, nVal))
      {
          // TO DO: do stuff with value here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-10
        • 2021-06-16
        • 1970-01-01
        相关资源
        最近更新 更多