3

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?

4
  • 4
    Use braces, just like you normally would. Commented Oct 24, 2013 at 13:29
  • @chris: As in Myclass m = {"hello", "world"}; or as in Myclass(const std::string& c1, const std::string& c2) : commands{c1, c2}{}? Commented Oct 24, 2013 at 13:38
  • @thokra, One of those won't initialize the members, while the other is displayed nicely in Vaughn's answer. Commented Oct 24, 2013 at 13:41
  • @chris: Yes, that was sort of what I was getting at. ;) Commented Oct 24, 2013 at 13:45

4 Answers 4

12

With C++11, you do it like this:

class MyClass{
           private:
           std::string commands[2];
           //std::vector<std::string> commands;

           public:
           MyClass( std::string command1, std::string command2)
             : commands{command1,command2}
           {/* */}
};

For pre-C++11 compilers, you will need to initialize the array or vector in the body of the constructor:

class MyClass{
           private:
           std::string commands[2];

           public:
           MyClass( std::string command1, std::string command2)
           {
               commands[0] = command1;
               commands[1] = command2;
           }
};

or

class MyClass{
           private:
           std::vector<std::string> commands;

           public:
           MyClass( std::string command1, std::string command2)
           {
               commands.reserve(2);
               commands.push_back(command1);
               commands.push_back(command2);
           }
};
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. And for older compilers?
@fotinsky: I've extended my answer to cover pre-C++11 compilers.
@VaughnCato, what if I use C++11 compilers and define a private member std::vector<std::string> commands; as you did in the pre-C++ example? Is there a faster way to initialize commands?
@leofang yes. It's exactly the same either way.
1

In the initializer list you may call any constructor of the class of the member you want to initialize. Take a look at std::string and std::vector documentation and choose the constructor that works for you.

For storing two objects I would recommend using std::pair. However if you expect that the number may grow std::vector is the best option.

Comments

1

You could use

#include <utility>

...
std::pair<string, string> commands;
commands=std::make_pair("string1","string2");

...
//to access them use
std::cout<<commands.first<<" "<<commands.second;

Comments

1
class Myclass{
private:
    Vector<string> *commands;
    // std::vector<std::string> commands(2); respectively 

    public:
    MyClass( std::string command1, std::string command2)
    {
        commands.append(command1);  //Add your values here
    }
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.