0
class Foo{
static vector<Point> vec[3];
};

So I have an array of three vectors. I need each vector to be initialized at compile time.

For example something that looks like this:

vec[0] = { {1,2}, {3,4}}; // contain two points
vec[1] = { {0, 0}};       // contain one point
vec[2] = {};              // empty

Is it possible to do this?

4 Answers 4

1

you need to declare it in the cpp file with the intialization list

in the cpp file:

vector<Poiint> Foo::vec={Point(1,2,3), Point(4,5,6), Point(8,9,10)};

where Point(x,y,z) is the constructor so it will populate the vector with three elements

in the class you should declare it like that:

static vector<Point> vec;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the hints, but I did mean to have three vectors (as an array), not one:)
0

A work-around would be adding an internal static Class _init (name it the way you like), whose constructor performs the actual initializing.

Class Foo{
public:
static int bar;
static class _init{
 public _init(){// do something to bar}
} Initializer;
};

// --- in .cpp
// define 'bar' and 'Initializer'

So that Initializer's constructor will be called to initialize bar.

Comments

0

It looks like the intention is more like:

static vector < vector<Point> > vec;


If so, then with some short testing in the C++11, Windows 64-bit C++Builder compiling and running, that obtains the desired result. As an additional experiment, after defining parameter-less and passed value constructors for the Point (class?) type used in the example, calling push_back on the multidimensional vec works as well:

vec.push_back( {{1,2}, {3,4}} );
vec.push_back( {{0,0}} );
vec.push_back( {{4,5}} );

I am confident the reason the Point class can be neglected is because the multidimensional vec vector has been declared to store Point instances. The last line in the OP above of passing an empty Point does not achieve anything, as can be shown when traversing the vec. The third element (index two), will not print a thing. The {4, 5} Point has been added to test traversing to all indices of the vec. Notice that there are two sets of curly braces inside the call of push_back. Without the outer set of curly braces, the compiler error says, "no matching member function for call to push_back."

Sample code to traverse the multidimensional vector (e.g. matrix or array):

//
// Declared a typedef to separate the vector row and the whole matrix
// Makes it simpler to traverse each vector row later.  intVals is a 
// test class with public integer elements "a" and "b".  The intVals
// class substitutes for the Point type in the OP above.
//
typedef vector<intVals> vec_t;
vector<vec_t> matrix;


int jj;
for (int i = 0; i < 3; i++)
{  
   jj = 0;
   for (vec_t::iterator j = matrix[i].begin(); j != matrix[i].end(); j++)
   {
      cout << "Matrix at index: " << i << ", element: " << jj << " a = " << (*j).a;
      cout << " b = " << (*j).b << endl;
      jj++;
   }
}                                                  


Note that the outer loop is clamped at three. The real implementation could use the matrix::iterator instead (or a range for).

For additional reference on a multidimensional vector see the multidimensional vector, how to forum posting.

Comments

0
    vec[0] = { {1,2}, {3,4}};

This won't work as there is no vector constructor that takes variable number of objects (works in C++11 with support of initializer_list).

To get around that, you can do this using a couple of arrays:

    Point v1[2] = {Point(1,2), Point(3,4)};
    Point v2[1] = {Point(0.0)}; 

vector<Point> Foo::vec[] = {vector<Point>(v1, v1+2), vector<Point>(v2, v2+1), vector<Point>()};

This uses the vector constructor that takes the begin and end iterators to construct the vector.

In C++11, I think you can do it this way:

    vector<Point> Foo::vec[] = {{Point(1,2), Point(3,4)}, {Point(0,0)}, {}};

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.