【问题标题】:Add map object to Vector in(c++) cocos2dx在 (c++) cocos2dx 中将地图对象添加到 Vector
【发布时间】:2015-07-08 08:06:10
【问题描述】:

这是我要添加地图对象的向量

  std::vector<cocos2d::Value> optionButton = std::vector<cocos2d::Value>();


 for (int i = 0; i < answerButton.size(); i++)
{
    value = answerButton.at(i).getDescription();

    if(strcmp(value.c_str()," ") == 0)
    {
        CCLOG("Space Detected %s", "Space Detected");

    }
    else
    {
        std::map<std::string ,std::string> keyval;
        keyval["letter"] = value;
        keyval["is_actual"]= "true";

        optionButton.push_back(keyval);            
    }

我想将“keyval”对象添加到“optionButton”向量。这可能吗??

【问题讨论】:

    标签: c++ cocos2d-x cocos2d-x-3.0


    【解决方案1】:

    请验证这是否有效?

    std::vector<std::unordered_map> optionButton = std::vector<std::unordered_map>();
    for (int i = 0; i < answerButton.size(); i++){
      value = answerButton.at(i).getDescription();
    
      if(strcmp(value.c_str()," ") == 0) CCLOG("Space Detected %s", "Space Detected");
      else {
        std::unordered_map<std::string ,std::string> keyval;
        keyval["letter"] = value;
        keyval["is_actual"]= "true";
    
        optionButton.push_back(keyval);
      }
    }
    

    【讨论】:

      【解决方案2】:

      您应该改用std::unordered_map

      来自the reference

      cocos2d::Value 是许多原语的包装类(intfloatdoubleboolunsigned charchar*std::string) 加上std::vector&lt;Value&gt;std::unordered_map&lt;std::string,Value&gt;std::unordered_map&lt;int,Value&gt;.

      您不必对代码进行太多更改。

      我没有尝试,但我认为这应该可行(完全基于我引用的内容):

      // ...
      std::unordered_map<std::string, cocos2d::Value> keyval;
      keyval["letter"] = value;     // I don't know what type is value
      keyval["is_actual"] = "true"; // Implicit conversion
      
      optionButton.push_back(keyval);
      // ...
      

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 2012-03-11
        • 2013-04-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多