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

Asked

Viewed 192 times

0

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

I have a CICLO struct, 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 I’d have a cycle vector to store all the cycles ever cracked.

vector<CICLO> ConjCiclos;

For each new cycle created, I need to check that it is no longer stored in Conjciclos.
O ciclo 1-2-2-1 é o mesmo que o ciclo 2-2-1-2. How can I detect it efficiently? I thought using a map would be a good option. However, what key lófica can I create, so that the above cycles have the same key?

1 answer

-1

I suggest using unordered_map, which has a constant search cost O(1), 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]
Follow an example of use:

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
}
  • But how could I compose a key in that case? You have any suggestions?

  • What parameters do you set as a key for searching?

  • I am in need of help precisely to define this key. Given two equal cycles, more created differently, for example 1-2-3-4-1 and 2-3-4-1-2. How can I set the key to the cycles, so that these cycles have the same key and I can detect redundancy?

  • You can use this sequence of numbers to assemble a single number, concatenating them, so they will be unique to each Cycle. If these sequences are larger, not fitting in a LONG, for example, pass them to a Std:string buffer, placing each one in a position. Since Std:string is a class you can adjust each one to have the required size and not worry about memory release.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.