-1

I have a

class myclass
{
    // ...
    static const vector<pair<string,vector<string>>> var;
    // ...
};

in the class definition, to use mappings of single strings to several others. I am using vector<> instead of arrays, in order to be able to add mapping pairs and mapping lengths without having to mantain size variables as well. Is there a way to initialize the variable in the corresponding .cpp file like a vector of a non composite type, that is, in the format:

 const vector<pair<string,vector<string>>> myclass :: var =
{
   ???
}

or do I have to use a static method, like

static myclass::initStaticMembers(){...}

If there is a way to do it in the 1st way, what is the syntax? I have searched, but did not find the syntax to do the composite std::pair initialization. For example, you can init a vector<string> with

vector <string>myvec={"elem1", "elem2","elem3"};

but how do you init the complex vector<pair<string,vector<string>>> ? Thank you.

npit
  • 2,219
  • 2
  • 19
  • 25
  • This has been asked many times. y u no search? – Lightness Races in Orbit Aug 03 '15 at 14:27
  • Yes, there is. Why don't you make use of it then? – edmz Aug 03 '15 at 14:27
  • possible duplicate of [C++ where to initialize static const](http://stackoverflow.com/questions/2605520/c-where-to-initialize-static-const) – VP. Aug 03 '15 at 14:29
  • I have searched, but did not find the syntax to do the composite std::pair initialization. For example, you can init a vector with `vector myvec={"elem1", "elem2","elem3"};` , but how do you init the complex `vector>>` ? Will update original post with this comment. – npit Aug 03 '15 at 14:31
  • @npit Curly braces initialization can be nested. Try something like `{ {"pair1", {"element11", "element12"}}, {"pair2", {"element21", "element22"}} }` – KABoissonneault Aug 03 '15 at 14:37
  • @VictorPolevoy thanks for the link, but I know where I should initialize it, my question is what is the syntax to do it, if it is possible to be done without an initializer function. – npit Aug 03 '15 at 14:38

2 Answers2

6

Simple as always - just logically nest each entity with it's initializer list and with use of implicit conversions. For example, I've worked with your code a bit and made this example:

class A {
    static const std::vector<std::pair<std::string, std::vector<std::string>>> var;
};

const std::vector<std::pair<std::string, std::vector<std::string>>> A::var = {
    {"abc", {"def", "ghj"}}
};

Just when you write initialization with initiliazer lists think about each entity from left to right in the template:

  1. std::vector = needs {ELEM}. Result is {ELEM}.
  2. Inside std::vector - an std::pair which also needs {FIRST, SECOND}. Result is {{FIRST, SECOND}}. .. and so on.

So, imagine it like this:

std::vector<std::pair<std::string, std::vector<std::string>>>
     ^      ^         ^            ^           ^        ^
     |      |         |            |           |        |

     {      {         "abc"        {           "abc", "def"  }  }   }

     |      |                      |                         |  |   |
     |      |                      |--------vector-----------|  |   |
     |      |--------------------------pair---------------------|   |
     |---------------------------vector-----------------------------|
VP.
  • 15,509
  • 17
  • 91
  • 161
1

You need to nest the initializations

const std::vector<std::pair<std::string,std::vector<std::string>>> Foo::var = 
{ // start of vector
    { "pair1", {"one", "two"}},
    { "pair2", {"three", "four"}},
    { "pair3", {"five", "six"}}
};// end of vector

Then you could do something like:

class Foo  // this would be in the .h file
{
public:
    static const std::vector<std::pair<std::string,std::vector<std::string>>> var;
};

// this part would be in the .cpp file
const std::vector<std::pair<std::string,std::vector<std::string>>> Foo::var = 
{ // start of vector
    { "pair1", {"one", "two"}},
    { "pair2", {"three", "four"}},
    { "pair3", {"five", "six"}}
};// end of vector

int main()
{
    for (const auto & p : Foo::var)
    {
        std::cout << p.first << "\t";
        for (const auto & e : p.second)
        {
            std::cout << e << "\t";
        }
        std::cout << std::endl;
    }
    return 0;
}

Output:

pair1   one two 
pair2   three   four    
pair3   five    six 

Live Example

NathanOliver
  • 171,901
  • 28
  • 288
  • 402