2

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 (e.g. here: std::vector versus std::array in C++)

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. 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.

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.

Community
  • 1
  • 1

4 Answers4

2

Use resize:

vector1.resize(4, vector<double>(nn - 1));
Jarod42
  • 203,559
  • 14
  • 181
  • 302
0

Use resize() function as follows:

vector<vector<double>> v;

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

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] = ...;
}
Alexander Balabin
  • 2,055
  • 11
  • 13
  • yes, you are right. But actually your code simply provides me an one dimensional vector. So I need to figure out how to implement the second dimension. I already tried std::move(vector> (4, vector (nn - 1))) but it didnt work out. Also using a for loop where I write vector1[ii].push_back.. does not work * I programmed without std:: just not to confuse I wrote it here again – PublicUserName Jul 27 '15 at 15:05
  • ok.. I got the answer: Somehow I forgot the first for loop... thank you! – PublicUserName Jul 27 '15 at 15:14
  • @PublicUserName, there is no real multidimentional vector in standard c++. What you declare is one-dimentional vector of vectors which for most purposes is as good as mutlidimentional. My code fills the outer vector with 4 vectors of length nn-1 forming the data structure you need. – Alexander Balabin Jul 27 '15 at 15:17
-1

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.)
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252