views:

144

answers:

3

I have a multimap defined by

typedef std::pair<int, int> au_pair; //vertices
typedef std::pair<int, int> acq_pair; //ch qlty specified by C
typedef std::multimap<int, acq_pair> au_map;
typedef au_map::iterator It_au;

The no. of simulations depend on the size of the au_map. For eg: if the au_map.size() = 5 I will have C1, C2, C3, C4, C5. and therefore 2^5 = 32 cases.

For example: If the au_map.size()=4, I need to simulate my algorithm for 16 cases.

for(size_t i = 0; i != 16; ++i)
{
  for(It_au it = a_map.begin(); it != a_map.end();)
  {
    acq_pair it1 = it->second;
    //case 0:
    //C1 = 0, C2 = 0, C3 = 0, C4 = 0
    //@Matthieu M 's suggestion http://stackoverflow.com/questions/3110975/c-case-declaration-closed
    //bool const c1 = i & 1;
    //bool const c2 = i & 2;
    //bool const c3 = i & 4;
    //bool const c4 = i & 8;
    //Update it1.second with corresponding C values
    it->second.second = C1;
    it++;
    it->second.second = C2;
    it++;
    it->second.second = C3;
    it++;
    it->second.second = C4;
    it++;
  }
  //simulate algorithm
}

How can I automate this process, where the size of C changes according to the au_map.size()? Thus, I will have C1, C2, C3, C4 when au_map.size() = 4 and C1, C2, C3, C4, C5 when au_map.size() = 5.

Also, what's preferred a vector with these values or add this to a pair inside multimap? Vector lookup time is lesser than multimap.

Also, if I keep inserting values to a multimap, will the new/updated values be passed to the algorithm?

A: 

What are C1, C2, and so on? Are they just ints, or strings? In that case you could automatically generate them by keeping a counter variable.
Why would you want a pair<int,int> inside the multimap?
I don't understand the last question.

rturrado
A: 

No disrespect intended, but you ask the most confusing questions. I had to dig up your previous question to understand this one a little better and I'm not sure if I understood the previous question either.

Originally I have 4 inputs C1, C2, C3, C4. This means I have a total of 16 combinations:

0000 0001 . . . 1111

How can I automate this process, where the size of C changes [...]

Normally the simplest way is to write nested loops to generate the combinations (I know this isn't what you want, keep reading):

for (int a=0; a < 2; ++a)
{
    for (int b=0; b < 2; ++b)
    {
        for (int c=0; c < 2; ++c)
        {
            for (int d=0; d < 2; ++d)
            {
                // I'm just printing the values here but
                // you could insert them to a container if 
                // you want.
                cout << a << b << c << d << endl;
            }
        }
    }
}

However, if you can't determine the number of nested loops we need to write in advance (ex: if the size of C is based on runtime conditions), then consider a recursive solution to generate the combinations.

void generate_combinations(int depth, int max_depth, string str)
{
    if (depth < max_depth)
    {
        generate_combinations(depth + 1, max_depth, str + "0");
        generate_combinations(depth + 1, max_depth, str + "1");
    }
    else
        cout << str << " ";
}

int main()
{
    generate_combinations(0, 3, "");
}

This outputs:

000 001 010 011 100 101 110 111

While this:

generate_combinations(0, 4, "");

Outputs:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

... and so on. You can control C however you like based on runtime conditions and very easily so.

Also, what's preferred a vector with these values or add this to a pair inside multimap? Vector lookup time is lesser than multimap.

If your data is dense (ex: indices ranging from 0 to N with no gaps), then there's no reason to use a map with int keys. Using a map with integral keys is only useful if the data you want to represent is sparse. Otherwise consider std::vector or std::deque.

+1  A: 
andand
@andand I am using a multimap to store <key1,pair<key2, value2>>. C1 is set for a specific value of Key1 and Key2. And then the algorithm runs after than again the value of C1 changes and the algorithm executes. I understand your solution better, will try it out and post my results.
vivekv80
@vivekv80: If you have two keys that you want to pair together, why not use std::map<std::pair<key1, key2>, value> or std::multimap<std::pair<key1, key2>, value> instead? By splitting the key apart like that, you're making your implementation much harder than it needs to be. If you're concerned about an ordering on your key (i.e. std::pair<key1, key2>), it's very simple to create one by comparing the first elements. If first elements are equal, compare the second elements.
andand
@andand au_map is a multimap of vertices and edges in a graph. Depending on the size of the multimap, each vertex and edge is assigned a value C. This value ‘C' is accessed later in the algorithm and then used for calculating other values. I have never used `std::multimap<std::pair<key1, key2>, value>` before. Let me try this as well. Thx for the feedback.
vivekv80
@vivekv80: Okay, this is starting to become clearer. If key1 and key2 are vertices, and the value is an edge weight, and you only allow no more than one directional edge from v1 to v2, then you should definitely consider std::map instead of std::multimap. Of course if you allow multiple edges from v1 to v2, you will need to use multimap. You might also want to look at the boost graph library. If you're spending a lot of time working out how to manage graphs, you could save yourself some effort.
andand
@andand: I am using boost for doing my initial graph procedures. Based on your feedback I was looking at boost::associative_proprty_map Would that be a better way to look at this problem?
vivekv80
@vivekv80: What's the problem you're trying to solve specifically with the boost::associative_property_map? Is it to assign a property to each node or edge using BGL? If that's the case then that seems to be a reasonable approach to take.
andand