简体   繁体   中英

C++ case multimap

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?

Like others, I'm not certain I completely understand your question. It looks like all you need is a representation of the binary of each integer from 0 to 2 bits -1 that you can conveniently reference (in the cases you mention, bits is either 4 or 5, but you want to generalize). If that's the case, a simpler to manage and access structure will be a vector of bool vectors. That is, rather than using a std::multimap , for general values of bits , replace the std::multimap with a std::vector<std::vector<bool> > ... something like:

std::vector<std::vector<bool> > c_flags(1 << bits);

for (size_t i = 0; i < c_flags().size(); ++i)
{
    for (size_t j = 0; j < bits; ++j)
        c_flags[i].push_back( (i & (1 << j)) > 0);
}

At this point, c_flags[i] contains a vector of bools representing the binary digits of i where true and false correspond to 1 and 0 respectively.

You could also use a std::map<std::vector<bool> > instead of the std::vector<std::vector<bool> > which may reduce memory requirements (if you don't need all of the possible binary representations) at the expense of being more expensive computationally. I don't see why you need to use a std::multimap , but then I don't have a lot of insight into the specifics of the problem you're trying to address either.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM