I want to initialize the sizes of vectors in an array of objects.
Every vector have the same size so...
LIB FILE:
#include <vector>
Class NameClass
{
public:
explicit NameClass( unsigned size )
{
vectorName.resize( size );
}
std::vector <type> vectorName;
};
MAIN FILE:
#include "lib.hpp"
int main( void )
{
NameClass object( size ); #1
NameClass object[ number_objects ]( size ); #2
NameClass object[ number_objects ] = {NameClass(size), NameClass(size), ... }; #3
return 0;
}
The #1 works, but is not an array, The #2 doesn't, compiler says "conversion from int to non-scalar type 'NameClass' requested" And #3 works, but... it's just absurd initialize every object. and I can't just put a static to size inside the class, because the value change.
So... My research say that I need use std::generate. the question is... Whats is the best way?
Sorry if is easy question how to use std::generate I'm beginner and get troubles to find the optimal solution.
Some people suggest complex solutions but, I keep using my solution
#include "lib.hpp"
int main( void )
{
unsigned number_objects = something;
unsigned size = other_thing;
NameClass object[ number_objects ];
for( unsigned i = 0; i < number_objects; i++)
object[i].vectorName.resize( size );
return 0;
}
I use thins because is really easy to understand, and works. But I'm open to other easy to understand and functional solutions.