How to use map / create keys to store cycles c / c ++?

0

What is the most efficient c / c ++ framework I can use to create and store cycles, so that I ensure that repeated cycles are not stored?

I have a struct CYCLE, such that:

struct CICLO {
    vector<Arco> rota;
    float COST;
}

struct Arco {

    int i, j;
    Arco () {};
    Arco (const Arco& obj): i(obj.i), j(obj.j) {};
    Arco(int _i, int _j) : i(_i), j(_j) {}    

};

I thought about having a cycle vector to store all the already created cycles.

vector<CICLO> ConjCiclos;

For each new cycle created, I need to check if it is no longer stored in Conjuncts. The 1-2-2-1 cycle is the same as the 2-2-1-2 cycle. How can I detect this efficiently? I thought using map would be a good option. However, what key logic can I create, such that the above cycles have the same key?

    
asked by anonymous 27.03.2018 / 16:25

1 answer

-1

I suggest using unordered_map, which has a constant O (1) search cost, as long as you can compose a unique search key for each search. For example, if your key is a string:

typedef std::unordered_map<std::string, CICLO> map_ciclos_type;

[EDIT]
Here is an example usage:

map_ciclos_type map_ciclos;
map_ciclos_type::iterator iter_map_ciclos;

std::string chave("1-2-2-1-1");
CICLO ciclo;
map_ciclos[chave] = ciclo;

iter_map_ciclos = map_ciclos.find(chave);

if (iter_map_ciclos == map_ciclos.end())
{
    //não encontrou
}
else
{
    //ciclo existe
}
    
27.03.2018 / 17:54