簡體   English   中英

將std :: map的數據拆分為多個std :: vector

[英]splitting data of a std::map into multiple std::vectors

我有兩個類:InitTable和InitEntry。 InitTable包含一個std::map table ,該std::map table存儲條目(汽車)的ID ,以及代表該汽車的對象( InitEntry )。 每個InitEntry都有3個成員變量:

  1. std::string ID
  2. double speed
  3. std::string heading

我的目標是首先將所有汽車存儲在std::map table ,然后遍歷該數據結構,然后嘗試基於常見屬性( speedheading )將汽車組織到群集( std::vector<InitEntry> )中。

例:

假設我們有6輛車(編號0到8)

  • car0:id:“ 0”,速度:22,標題為“ N”
  • car1:id:“ 1”,速度:26,標題為“ N”
  • car2:id:“ 2”,速度:28,標題為“ 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 ++中實現這一點。


InitTable.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;
    };

InitTable.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 ... etc

考慮更改您的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();});

顯然,您需要添加吸氣劑才能完成這些工作。

如果您不想遍歷所有元素(我想您不是),那么唯一的選擇就是將這些組保留在某個位置,並且在主表中僅保留一些指向它們的索引。 大致如下所示:

#include <string>
#include <vector>
#include <map>
#include <cassert>
#include <algorithm>

typedef std::string ID;
typedef std::string Heading;
typedef double Speed;

struct Entry
{
    ID id;
    Speed speed;
    Heading heading;
};

class EntryProxy
{
public:
    EntryProxy(Entry* target) : entry(target)
    {}

    ID getId()
    {
        return entry->id;
    }

    Speed getSpeed()
    {
        return entry->speed;
    }

    Heading getHeading()
    {
        return entry->heading;
    }

private:
    Entry* entry;
};

class InitTable
{
public:
    const std::vector<EntryProxy>& getSameHeading(std::string id)
    {
        return groupedByHeadings.at(entries.at(id).heading);
    }

    const std::vector<EntryProxy>& getSimilarSpeed(std::string id)
    {
        return groupedBySpeed.at(calculateSpeedGroupIndex(entries.at(id).speed));
    }

    void addEntry(ID id, Speed speed, Heading heading)
    {
        Entry e{ id, speed, heading };
        auto record = entries.insert(std::make_pair(id, e)); // pair<iterator, bool>

        groupedByHeadings[record.first->second.heading].push_back(EntryProxy(&record.first->second));
        groupedBySpeed[calculateSpeedGroupIndex(record.first->second.speed)].push_back(EntryProxy(&record.first->second));
    }

    void deleteEntry(ID id)
    {
        auto entry = entries.find(id);
        assert(entry != entries.end());

        auto currentEntryHeadings = groupedByHeadings[entry->second.heading];
        currentEntryHeadings.erase(std::find_if(currentEntryHeadings.begin(), currentEntryHeadings.end(), [&entry](EntryProxy p){return p.getId() == entry->second.id; }));

        auto currentEntrySpeeds = groupedBySpeed[calculateSpeedGroupIndex(entry->second.speed)];
        currentEntrySpeeds.erase(std::find_if(currentEntrySpeeds.begin(), currentEntrySpeeds.end(), [&entry](EntryProxy p){return p.getId() == entry->second.id; }));

        entries.erase(id);
    }

    EntryProxy getEntry(ID id)
    {
        return EntryProxy(&entries.at(id));
    }

private:
    typedef int SpeedGroupIndex;

    SpeedGroupIndex calculateSpeedGroupIndex(Speed s)
    {
        // you may prefer a different implementation here

        return s / 10;
    }

    std::map<ID, Entry> entries;
    std::map<Heading, std::vector<EntryProxy> > groupedByHeadings;
    std::map<SpeedGroupIndex, std::vector<EntryProxy> > groupedBySpeed;
};

按標題分組很明顯,因為它只需要一個簡單的地圖。 但是,在速度方面要困難一些-您需要描述分組的要求(通過實現getSpeedGroupId函數),因為不應將double用作地圖索引。

如果您願意向內存管理更安全的方法可以替代Entry*std::shared_ptr<Entry>EntryProxyEntrystd::shared_ptr<Entry>InitTable::entries 在這種情況下,您還需要將創建從Entry e{id, speed, heading更改為auto e = std::make_shared<Entry>(id, speed, heading)

如果刪除對您來說太復雜,則可以將EntryProxy結構保留在列表中,並在entries映射中引用特定的迭代器。

您可能還不喜歡組是EntryProxy而單個元素是Entry的事實,在這種情況下,您可以使getEntry也返回EntryProxy

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM