How can I initialize a (string) array or vector with a constructor's initializing list in C++?
Consider please this example where I want to initialize a string array with arguments given to the constructor:
#include <string>
#include <vector>
class Myclass{
private:
std::string commands[2];
// std::vector<std::string> commands(2); respectively
public:
MyClass( std::string command1, std::string command2) : commands( ??? )
{/* */}
}
int main(){
MyClass myclass("foo", "bar");
return 0;
}
Besides that, which of the two types (array vs vector) is recommended for saving two strings while creating an object, and why?
Myclass m = {"hello", "world"};or as inMyclass(const std::string& c1, const std::string& c2) : commands{c1, c2}{}?