views:

360

answers:

1

For example, a class named Table, with its constructor being: Table(string name="", vector <string> mods);

How would I initialize the vector to be empty?

Edit: Forgot to mention this was C++.

+2  A: 
Table(string name="", vector <string> mods);

if you want vector to be empty inside constructor then

mods.clear();

or

mods.swap(vector<string>());

In case you want as a default parameter:

 Table(string name="", vector<string> mods = vector<string>());

Like any other default parameter.

aJ
Ahh thanks! I kept trying things like vector <string> mods(0,"") and many other variations. Wouldn't have guessed this at all!
Omar
Caught your message before you edited it, this method works vector <string> mods = vector<string>() and just vector mods = vector() complains about needing a template argument.I'm glad I saw the first message because I probably wouldn't have figured out to put the template argument in both sides of the assignment.
Omar
sorry, it was not formatted properly.
aJ