How to initialize a vector of a pair of strings, strings in a C ++ class?

How to initialize a line pair vector in a C ++ class? I tried a few things but no one worked.

vector<pair<string,string>> myVec(); //doesn't work 
+5
source share
3 answers

There is no need to initialize the vector in this way

 vector<pair<string,string>> myVec(); 

This is a function declaration called myVec , which has no parameters and has a return type vector<pair<string,string>>

Just write

 vector<pair<string,string>> myVec; 

because in any case you are creating an empty vector.

Or, if you want the vector to have some initial values, and your compiler supports C ++ 2011, then you can also write

 std::vector<std::pair<std::string, std::string>> myVec = { { "first", "first" }, { "second", "second" }, { "third", "third" } }; 
+10
source

If you use () , you will come across the most unpleasant analysis . You have declared the myVec function, which takes no arguments and returns vector<pair<string, string>>

Switch to {}

 vector<pair<string,string>> myVec{}; 
+9
source

Try using it like this, now your myVec function has no parameters and returns vector<pair<string,string>> :

 vector<pair<string,string>> myVec{}; 

or

 vector<pair<string,string>> myVec; 
+2
source

All Articles