简体   繁体   English

C ++类变量:向量的初始化与赋值和初始化

[英]C++ Class Variables: Initialization vs. Assignment and Initialization of vectors

I am working on a C++ program that has a series of class variables that contain vectors on some or all of the member variables. 我正在一个C ++程序上工作,该程序具有一系列类变量,这些类变量包含一些或所有成员变量上的向量。 My question is three-fold: 我的问题有三点:

  1. Is it straight-forward to use constructors to initialize vector variables that are part of a class (see sample class definition below)? 使用构造函数初始化属于类的向量变量是否简单(请参见下面的示例类定义)? Could someone post an example constructor for the class below (or for at least the single and two-dimension vector variables)? 有人可以为下面的类(或至少一维和二维向量变量)发布示例构造函数吗?

  2. Is there a problem with simply initializing the variables myself in my code (ie, iterating through each element of the vectors using loops to assign an initial value)? 在我自己的代码中自己简单地初始化变量是否存在问题(即使用循环分配初始值来遍历向量的每个元素)?

  3. Along the same lines, if the variables need to be initialized to different values in different contexts (eg, zero in one instance, some number in another instance), is there a way to handle that through constructors? 同样,如果变量需要在不同的上下文中初始化为不同的值(例如,一个实例中为零,另一个实例中为某个数字),是否可以通过构造函数来处理该变量?

Sample class definition: 示例类定义:

class CreditBasedPoolLevel {
public:
    int NumofLoans; 
    int NumofPaths; 
    int NumofPeriods; 
    double TotalPoolBal;

    vector<int> NumofModeled;
    vector<double> ForbearanceAmt;
    vector<double> TotalAmtModeled;

    vector<vector<int>> DefCountPoolVector;
    vector<vector<double>> TermDefBalPoolVector;
    vector<vector<double>> BalloonDefBalPoolVector;
    vector<vector<double>> TermDefBalPoolVectorCum; 
    vector<vector<double>> TermSeverityAmt;
    vector<vector<double>> TermELAmtPoolVector;
    vector<vector<double>> BalloonELAmtPoolVector;
    vector<vector<double>> TermELAmtPoolVectorCum;

};

As for question number three, simply add a constructor with an argument that is the value you want to initialize the vectors with. 对于第三个问题,只需添加一个带有参数的构造函数,该参数就是您要用来初始化向量的值。

And if you just want the vectors to be default constructed, then there's nothing that needs to be done. 而且,如果您只想默认构造矢量,则无需执行任何操作。

Constructor may look something like this: 构造函数可能看起来像这样:

CreditBasedPoolLevel::CreditBasedPoolLevel()
{
   const int numDefCountPools = 13;
   const int numDefCountPoolEntries = 25;

   for(int i = 0; i < numDefCountPools; i++)
   {
      vector<int> v;
      for(int j = 0; j < numDefCountPoolEntries; j++)
      {
          v.push_back(j + i * 5);  // Don't know what value you ACTUALLY want to fill here
      }
      DefCountPoolVector.push_back(v);
   }
}

Note that this is ONE solution, it really depends on what values you want, how you went them organized, etc, what is the "right" solution for your case. 请注意,这是一个解决方案,它实际上取决于您想要什么值,如何组织它们,等等,什么是适合您的案例的“正确”解决方案。

  1. In C++, initializing a variable calls its constructor. 在C ++中,初始化变量将调用其构造函数。 In a vector's case, this means it creates an instance of a vector with whatever the initial capacity is (10 I believe), with no values. 在向量的情况下,这意味着它将创建一个向量的实例,无论其初始容量是多少(我认为是10),都没有值。 At this point, you need to use push_back in order to fill the vector - even though it has a capacity, it will cause undefined behavior if you try to access unfilled areas directly (such as with NumofModeled[0]). 此时,您需要使用push_back来填充矢量-即使它具有容量,但是如果您尝试直接访问未填充区域(例如使用NumofModeled [0]),它将导致未定义的行为。 You can also initialize it with some amount of space by using vector NumofModeled(x) (x being the number of spaces), but generally because vectors have dynamic size, it's easier to use push_back unless there is some reason you need to enter your data out of order. 您还可以通过使用向量NumofModeled(x)(x为空格数)来以一定的空间初始化它,但是通常由于向量具有动态大小,因此除非有某些原因需要输入数据,否则使用push_back会更容易乱序。

  2. Relates to the capacity part of one, if you try to access unfilled space in a vector you will get undefined behavior. 与容量的一部分有关,如果尝试访问向量中的未填充空间,则会得到未定义的行为。 It's pretty standard practice to fill a vector with a loop though, such as: 虽然用循环填充向量是一种非常标准的做法,例如:

     vector<int> v; int in = 0; while (cin) { cin >> in; v.push_back(in); } 
  3. Yes, but remember that like functions, constructors only differentiate by the type of input parameters. 是的,但是请记住,就像函数一样,构造函数仅根据输入参数的类型来区分。 So, for example, you could have CreditBasedPoolLevel(int level) and CreditBasedPoolLevel(vector<int> levels) , but not another with the definition CreditBasedPoolLevel(int otherint) , because it would conflict with the first. 因此,例如,您可能拥有CreditBasedPoolLevel(int level)CreditBasedPoolLevel(vector<int> levels) ,但没有另一个具有CreditBasedPoolLevel(int otherint)定义的,因为它会与第一个冲突。 If you want to be able to take different contextual input of the same type, you can use another variable to define the constructor type, such as CreditBasedPoolLevel(int input, string type) and use a switch block to define the initialization logic based on the type. 如果您希望能够采用相同类型的不同上下文输入,则可以使用另一个变量来定义构造函数类型,例如CreditBasedPoolLevel(int input, string type)并使用一个switch块基于来定义初始化逻辑。类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM