c++ - 将 std::map 的数据拆分为多个 std::vectors

标签 c++ dictionary vector stl c++98

我有两个类:InitTable 和 InitEntry。 InitTable 包含一个 std::map table其中存储 ID条目(汽车)和代表该汽车的对象( InitEntry )。每个InitEntry有3个成员变量:

  1. std::string ID
  2. double speed
  3. 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/

相关文章:

C++;是否可以在子构造函数中间调用父构造函数?

java - 根据键和值对映射进行排序

c++ - 类 : handling copy constructor and destructor (C++) 内的 vector

C# 创建 int 和数组的字典

java - 如何从Map中获取最近100条数据?

c++ - 调用使用 push_back 添加到 vector 成员的成员函数时, vector 成员的大小始终为零

haskell - 如何在 Haskell 中将函数映射到向量的特定范围

c++ - OpenGL 网站中的 Phong 照明

c++ - 使用基于概念的递归函数模板在扣除 'auto [...] ' 之前使用 'auto'

c++ - 具有相同名称和签名的多个函数会相互覆盖吗?