gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:10:25 25 4
gpt4 key购买 nike

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

     {"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

最佳答案

从您的问题看来,您希望以 (key,value) 格式存储记录。为此,您可以将非键列封装在一个对象中,并将其存储在像 (key, object*) 这样的多重映射中。

我已经修改了你的代码,如下所示-

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

关于c++ - 如何在 C++ 中创建分组多映射键和关联值表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45695354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com