简体   繁体   English

如何在C ++中初始化一个空的全局向量

[英]How to initialize an empty global vector in C++

I have a general question. 我有一个一般性的问题。 Hopefully, one of you has a good approach to solve my problem. 希望你们中的一个有解决我问题的好方法。 How can I initialize an empty vector? 如何初始化空向量?

As far as I read, one has to know the size of an array at compiling time, though for vectors it is different. 据我所读,在编译时必须知道数组的大小,尽管对于向量而言却有所不同。 Vectors are stored in the heap (eg here: std::vector versus std::array in C++ ) 向量存储在堆中(例如,此处: std :: vector与Cd中的std :: array

In my program I want to let the client decide how accurate interpolation is going to be done. 在我的程序中,我想让客户决定要如何精确地进行插值。 That's why I want to use vectors. 这就是为什么我要使用向量。

The problem is: For reasons of clear arrangement I want to write two methods: 问题是:为了安排清楚,我想编写两个方法:

  • one method for calculating the coefficients of an vector and 一种计算向量系数的方法,以及

  • one method which is providing the coefficients to other functions. 一种将系数提供给其他函数的方法。

Thus, I want to declare my vector as global and empty like 因此,我想将向量声明为全局和空

vector<vector<double>> vector1;
vector<vector<double>> vector2;

However, in the method where I determine the coefficients I cannot use 但是,在我确定系数的方法中,我无法使用

//vector containing coefficients for interpolation
/*vector<vector<double>>*/   vector1 (4, vector<double>(nn - 1));
for (int ii = 0; ii < nn - 1; ii++) {vector1[ii][0] = ...;
}

"nn" will be given by the client when running the program. 客户端在运行程序时将给出“ nn”。 So my question is how can I initialize an empty vector? 所以我的问题是如何初始化一个空向量? Any ideas are appreciated! 任何想法表示赞赏!

Note please, if I call another function which by its definition gives back a vector as a return value I can write 请注意,如果我调用另一个函数,该函数根据其定义返回一个向量作为返回值,我可以编写

vector2= OneClass.OneMethod(SomeInputVector);

where OneClass is an object of a class and OneMethod is a method in the class OneClass. 其中OneClass是类的对象,而OneMethod是类OneClass中的方法。

Note also, when I remove the comment /**/ in front of the vector, it is not global any more and throws me an error when trying to get access to the coefficients. 还要注意,当我删除向量前面的注释/ ** /时,它不再是全局的,并且在尝试访问系数时抛出错误。

使用resize

vector1.resize(4, vector<double>(nn - 1));

Use resize() function as follows: 使用resize()函数,如下所示:

vector<vector<double>> v;

int f(int nn){
   v.resize(4);
   for(int i = 0; i < 4; i++){
      v[i].resize(nn - 1);
   }
}

It look to me that you're actually asking how to add items to your global vector. 在我看来,您实际上是在问如何向全局向量添加项目。 If so this might help: 如果是这样,这可能会有所帮助:

//vector containing coefficients for interpolation
for (int i = 0; i < 4; ++i)  
    vector1.push_back(vector<double>(nn - 1));

for (int ii = 0; ii < nn - 1; ii++) 
{
    vector1[ii][0] = ...;
}

Unsure if it is what you want, but assign could be interesting : 不确定是否是您想要的,但是分配可能很有趣:

vector<vector<double>> vector1; // initialises an empty vector

// later in the code : 
vector<double> v(nn -1, 0.); // creates a local vector of size 100 initialized with 0.
vector1.assign(4, v);  // vector1 is now a vector of 4 vectors of 100 double (currently all 0.)

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

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