我有两个类:InitTable 和 InitEntry。 InitTable 包含一个 std::map table
其中存储 ID
条目(汽车)和代表该汽车的对象( InitEntry
)。每个InitEntry
有3个成员变量:
-
std::string ID
-
double speed
-
std::string heading
我的目标是首先将所有汽车存储在 std::map table
中,然后遍历该数据结构,并尝试根据共同属性将汽车组织成集群 ( std::vector<InitEntry>
):speed
& heading
.
示例:
假设我们有 6 辆车(id 0 到 8)
- car0:id:“0”,速度:22,标题“N”
- car1: id: "1", speed: 26, heading "N"
- car2: id: "2", speed: 28, heading "W"
- car3:id:“3”,速度:12,标题“E”
- car4:id:“4”,速度:10,标题“E”
- car5:id:“5”,速度:45,标题“S”
为简单起见,在现阶段对我来说,仅根据航向对汽车进行分组就足够了。结果将是:
std::vector clus1 = {car0, car1}
std::vector clus2 = {car2}
std::vector clus3 = {car3, car4}
std::vector clus4 = {car5}
不幸的是,我对 C++ STL 的了解不够,无法理解如何在 C++ 中实现这一点。
初始化表.h:
#include <InitEntry.h>
class InitTable {
public:
InitTable();
virtual ~InitTable();
void clearTable();
void addEntry(std::string ID, double speed, std::string heading);
void deleteEntry(std::string ID);
InitEntry* getEntry(std::string ID);
protected:
std::map<std::string, InitEntry*> table;
};
初始化表.cc:
#include"InitTable.h"
InitTable::InitTable(){}
InitTable::~InitTable()
{
clearTable();
}
void InitTable::clearTable()
{
this->table.clear();
}
void InitTable::addEntry(std::string ID, double speed, std::string heading)
{
InitEntry* newEntry = new InitEntry(ID, speed, heading);
std::cout<< "server::InitTable: vehicle registered to the init table" << newEntry << endl;
table.insert(std::make_pair(ID, newEntry));
}
void InitTable::deleteEntry(std::string ID)
{
InitEntry* ie = getEntry(ID);
if (ie != NULL)
{
table.erase(ID);
delete ie;
}
}
InitEntry* InitTable::getEntry(std::string ID)
{
std::map<std::string, InitEntry*>::iterator it = table.find(ID);
if (it != table.end())
{
return it->second;
}
else
{
std::cout << "such entry does not exist" << endl;
return NULL;
}
}
InitEntry.h:
class InitEntry {
public:
virtual ~InitEntry();
InitEntry(std::string ID, double speed, std::string heading);
std::string getID();
protected:
std::string sumoID;
double speed;
std::string heading;
};
InitEntry.cc:
#include "InitEntry.h"
InitEntry::InitEntry(std::string ID, double speed, std::string heading): ID(ID), speed(speed), heading(heading){}
InitEntry::~InitEntry(){}
std::string InitEntry::getID()
{
return this->ID;
}
编辑 1:添加额外描述(应 @TomaszLewowski 的要求)。
是的,我的目标是按航向然后根据速度将车辆组织成集群。因此,最初会有一大群车辆朝某个方向行驶,之后需要根据速度将其分成更多的集群。比方说:车辆向“北”行驶,速度:0 - 20 ... 速度:40 - 50 ...等
最佳答案
考虑更改您的 std::map<std::string, InitEntry*> table;
到:
std::vector<InitEntry> table;
然后您需要更改您的 *Entry
方法:
void InitTable::addEntry(std::string ID, double speed, std::string heading)
{
table.push_back(InitEntry(ID, speed, heading));
std::cout<< "server::InitTable: vehicle registered to the init table" << table.back() << endl;
}
void InitTable::deleteEntry(std::string ID)
{
auto it = remove_if(table.begin(), table.end(), [&](const auto& i){return i.getID() == ID;});
table.resize(distance(table.begin(), it));
}
InitEntry* InitTable::getEntry(std::string ID)
{
auto it = find_if(table.begin(), table.end(), [&](const auto& i){return i.getID() == ID;});
if (it != table.end())
{
return it->second;
}
else
{
std::cout << "such entry does not exist" << endl;
return NULL;
}
}
排序table
您需要决定按什么进行排序。
- 按 ID 排序:
std::sort(table.begin(), table.end(), [](const auto& first, const auto& second){return first.getID() < second.getID();});
- 按速度排序:
std::sort(table.begin(), table.end(), [](const auto& first, const auto& second){return first.getSpeed() < second.getSpeed();});
- 按标题排序:
std::sort(table.begin(), table.end(), [](const auto& first, const auto& second){return first.getHeading() < second.getHeading();});
显然,您需要添加 getter 才能使这些工作正常进行。
关于c++ - 将 std::map 的数据拆分为多个 std::vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27928267/