如何在c ++中创建分组多映射键和关联值的表

时间:2017-08-15 14:46:37

标签: c++ boost stl multimap

在下面的代码中,我在基于Group1和Group2插入地图中的元素后,使用equal_range将multimap键与关联值分组。 根据我的要求,我需要根据以下格式的键值关联在API接口中设置这些值。

即。

     {"group1","pointcloud_min_z", "int_t","24"};
     {"group1","pointcloud_max_z", "bool_t","true"};
     {"group2","pointcloud_max_z", "double_t","13"};
     {"group2","pointcloud_min_z", "double_t","20.0"};

现在的问题是如何根据多个分组的密钥数据创建表。在这种情况下,列将始终保持不变,但基于多个地图行中的数据插入/删除将被添加/删除。这里group1和group2是键。请建议选择容器和方法将组转换为表格。

代码段

#include <iostream>
#include <string>
#include <map>
using namespace std;

typedef multimap<string, string> StringToIntMap;
typedef StringToIntMap::iterator mapIter;

int main ()
{
    StringToIntMap mymap;

    mymap.insert(make_pair("Group2", "pointcloud_max_z"));
    mymap.insert(make_pair("Group1",  "pointcloud_min_z"));
    mymap.insert(make_pair("Group2", "double_t"));
    mymap.insert(make_pair("Group1",  "int_t"));
    mymap.insert(make_pair("Group2", "13"));
    mymap.insert(make_pair("Group1",  "pointcloud_min_z"));
    mymap.insert(make_pair("Group2",  "pointcloud_min_z"));
    mymap.insert(make_pair("Group1",  "bool_t"));
    mymap.insert(make_pair("Group1",  "true"));
    mymap.insert(make_pair("Group1",  "pointcloud_min_max_z"));

    cout << "mymap contains:" << endl;

    mapIter m_it, s_it;

    for (m_it = mymap.begin();  m_it != mymap.end();  m_it = s_it)
    {
        string theKey = (*m_it).first;

        cout << endl;
        cout << "  key = '" << theKey << "'" << endl;

        pair<mapIter, mapIter> keyRange = mymap.equal_range(theKey);

        // Iterate over all map elements with key == theKey

        for (s_it = keyRange.first;  s_it != keyRange.second;  ++s_it)
        {
           cout << "    value = " << (*s_it).second << endl;
        }
    }

    return 0;

}   //  end main

enter code here

1 个答案:

答案 0 :(得分:1)

根据您的问题,您似乎希望以格式(键,值)存储记录。为此,您可以将非键列封装在对象中,并将其存储在多图(如键,对象*)中。

我修改了您的代码,如下所示 -

class DATA
{
public:
   //control access to data members as needed
   string name;
   string type;
   string value;

   DATA(string name, string type, string value): name(name), type(type), value(value) {}   
 };

typedef multimap<string, DATA*> StringToIntMap;
typedef StringToIntMap::iterator mapIter;

int main ()
{
    StringToIntMap mymap;

    //insert record {"group1","pointcloud_min_z", "int_t","24"}
    DATA *elem = new DATA("pointcloud_min_z", "int_t","24");
    mymap.insert(make_pair("Group1", elem));
    //insert record {"group1","pointcloud_max_z", "bool_t","true"}
    elem = new DATA("pointcloud_max_z", "bool_t","true");
    mymap.insert(make_pair("Group1", elem));
    //insert record {"group2","pointcloud_max_z", "double_t","13"}
    elem = new DATA("pointcloud_max_z", "double_t","13");
    mymap.insert(make_pair("Group2", elem));
    //insert record {"group2","pointcloud_min_z", "double_t","20.0"}
    elem = new DATA("pointcloud_min_z", "double_t","20.0");
    mymap.insert(make_pair("Group2", elem));

    cout << "mymap contains:" << endl;

    mapIter m_it, s_it;

    for (m_it = mymap.begin();  m_it != mymap.end();  m_it = s_it)
    {
        string theKey = (*m_it).first;

        cout << endl;
        cout << "  key = '" << theKey << "'" << endl;

        pair<mapIter, mapIter> keyRange = mymap.equal_range(theKey);

        // Iterate over all map elements with key == theKey

        for (s_it = keyRange.first;  s_it != keyRange.second;  ++s_it)
        {
           cout << "    value = " << (*s_it).second->name << ", " 
                << (*s_it).second->type << ", " 
                << (*s_it).second->value << endl;
        }
    }

    //when done delete the record pointers from the multimap
    return 0;

}   //  end main