c ++使用结构作为<map,<map,=“”vector =“”>&gt;的数据

时间:2016-04-21 20:14:26

标签: c++ dictionary vector struct

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

    //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)

2 个答案:

答案 0 :(得分:4)

您需要提供一个运营商&lt;要将您的结构用作地图的键,请参阅此问题:Struct as a key in a std::map

答案 1 :(得分:2)

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

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

我得到的编译器错误与你所说的非常不同。你发布了正确的代码吗?

以下是我看到的内容:https://godbolt.org/g/xl4zWw