【问题标题】:c++ using structs as data for a <map, <map, vector>>c++ 使用结构作为 <map, <map, vector>> 的数据
【发布时间】:2016-04-21 20:14:26
【问题描述】:

我在尝试加载具有另一个地图/矢量组合作为结构值的地图时遇到问题。下面是我尽量简化的代码:

    //These are the structs
    struct Order 
    {
        std::string A;
        std::string B;
    };

    struct Card
    {
        std::string C;
        std::string D;
    };

    struct Item  
    {
        std::string E;
        std::string F;
    };

    //this is the method that will read and load the map
    void LoadMap(ListofValues object)
    {
    std::map<Order, std::map<Item, std::vector<Card>>> records; //THIS is the data structure I'm trying to store into

    //ListofValues is a list passed that holds all these values that will need to be put into my map
    for(std::vector<ListofValues>::iterator vIter= object.begin(); vIter != object.end(); ++vIter)
            {           
                std::string sReceiptRecord = (*vIter).getReceipt(m_StdOrderConfirmVer);

                Order order = {(*vIter).getA,(*vIter).getB}; 
                Item item = {(*vIter).getC,(*vIter).getD}; 
                Card card = {wws::String((*vIter).getE), (*vIter).getF}; 

                records[order][item].push_back(card); //load into my map            
            }
      }

所以我将传递一个包含所有值列表(ListofValues)的对象。我将遍历该列表并使用 getter 方法,将这些值存储到结构中(getE 返回一个 Long,这就是必须进行转换的原因)。有没有我错过的步骤

我得到的错误是:

error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const Order' (or there is no acceptable conversion)

【问题讨论】:

  • 请发帖minimal reproducible example。你有一个编译器错误,如果我按原样使用你的代码,我会有很多。
  • 我认为您的班级订单需要一个运算符
  • 不确定他是如何得到他声称的错误的 - 他在他传入的变量的类型上调用 .begin() 和 .end()。真的需要 MCVE
  • @OP 看到 this?同样的错误,示例已完成。所有 C++ 错误都可以通过一个简单的示例重新创建,并且您应该分解代码以发现导致问题的原因,而不是使用向量、String、迭代器等...
  • 对不起,我在尝试将特定代码翻译成更简洁、通用的代码时感到困惑。为了得到相关的部分,我必须删除大约一百行。

标签: c++ dictionary vector struct


【解决方案1】:

您需要为您的结构提供一个运算符 Struct as a key in a std::map

【讨论】:

    【解决方案2】:

    ListOfValues是传递给LoadMap的参数的TYPE,object是实际变量。

    在你的 for 循环中,你需要说 object.begin()object.end()

    我得到的编译器错误与你所说的完全不同。您是否发布了正确的代码?

    这是我看到的:https://godbolt.org/g/xl4zWw

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-06
      • 2018-11-19
      • 2014-03-26
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      相关资源
      最近更新 更多