【问题标题】:Initialize array or vector over constructor's initializing list在构造函数的初始化列表上初始化数组或向量
【发布时间】:2013-10-24 13:28:46
【问题描述】:

如何在 C++ 中使用构造函数的初始化列表来初始化(字符串)数组或向量?

请考虑这个例子,我想用给构造函数的参数初始化一个字符串数组:

#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;
}

除此之外,在创建对象时建议使用两种类型(数组与向量)中的哪一种来保存两个字符串,为什么?

【问题讨论】:

  • 像往常一样使用大括号。
  • @chris: 如Myclass m = {"hello", "world"};Myclass(const std::string&amp; c1, const std::string&amp; c2) : commands{c1, c2}{}?
  • @thokra,其中一个不会初始化成员,而另一个在 Vaughn 的回答中很好地显示。
  • @chris:是的,这就是我的意思。 ;)

标签: c++ arrays vector constructor initialization


【解决方案1】:

使用 C++11,您可以这样做:

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

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

对于 C++11 之前的编译器,您需要在构造函数的主体中初始化数组或向量:

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

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

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);
           }
};

【讨论】:

  • 谢谢。对于较旧的编译器?
  • @fotinsky:我的答案已经扩展到 C++11 之前的编译器。
  • @VaughnCato,如果我使用 C++11 编译器并定义一个私有成员 std::vector&lt;std::string&gt; commands;,就像你在 C++ 之前的示例中所做的那样?有没有更快的方法来初始化commands
  • @leofang 是的。无论哪种方式都完全相同。
【解决方案2】:

在初始化列表中,您可以调用要初始化的成员的类的任何构造函数。查看std::stringstd::vector 文档并选择适合您的构造函数。

为了存储两个对象,我建议使用std::pair。但是,如果您预计这个数字可能会增长,std::vector 是最好的选择。

【讨论】:

    【解决方案3】:

    你可以使用

    #include <utility>
    
    ...
    std::pair<string, string> commands;
    commands=std::make_pair("string1","string2");
    
    ...
    //to access them use
    std::cout<<commands.first<<" "<<commands.second;
    

    【讨论】:

      【解决方案4】:
      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
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-04
        • 2020-12-27
        • 1970-01-01
        相关资源
        最近更新 更多