-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.

6
  • This has been asked many times. y u no search? Commented Aug 3, 2015 at 14:27
  • Yes, there is. Why don't you make use of it then?
    – edmz
    Commented Aug 3, 2015 at 14:27
  • possible duplicate of C++ where to initialize static const
    – VP.
    Commented Aug 3, 2015 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<string> with vector<string> myvec={"elem1", "elem2","elem3"}; , but how do you init the complex vector<pair<string,vector<string>>> ? Will update original post with this comment.
    – npit
    Commented Aug 3, 2015 at 14:31
  • @npit Curly braces initialization can be nested. Try something like { {"pair1", {"element11", "element12"}}, {"pair2", {"element21", "element22"}} } Commented Aug 3, 2015 at 14:37

2 Answers 2

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-----------------------------|
1
  • Thank you Victor, exactly what I needed.
    – npit
    Commented Aug 3, 2015 at 14:49
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.