1

Suppose I have the following piece of code:

#include <bits/stdc++.h>

using namespace std;

class SomeClass {
   private:
    const static map<int, int> some_map;
};

const map<int, int> SomeClass::some_map = {
    {1, 2},
    {3, 2},
    {4, 2},
    {10, 3},
    {11, 3},
    {12, 3},
    {15, 9}
};

As you can see, I initialize several keys of the map to the same value.

Would it be possible to express the same thing in any shorter syntax (i.e. something like: {1,3,4->2})? This is just a short example but in reality I have many keys with the same value and would like to retrieve this value quickly.

syntagma
  • 23,346
  • 16
  • 78
  • 134
  • There's nothing like this built in but you could write it yourself (with slightly different syntax). Just to be clear, your `std::map` is actually going to be initialized when your program starts up, it's not like an array that could actually be initialized "at compile time" in the sense that it could be baked into your object file. – mattnewport Sep 17 '15 at 23:45

1 Answers1

1

The desired syntax {1,3,4->2} looks like a set + value pair. You might use a vector instead of set. Then define your initialization data as a vector of such pairs, and add a spoonful of code to initialize your map from that.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331