How do I initialize a stl vector of objects who themselves have non-trivial constructors?
Solution 1
If the elements of the vector are not default-constructible, then there are certain things you cannot do with the vector. You cannot write this (example 1):
vector<MyInteger> foo(10);
You can, however, write this (example 2):
vector<MyInteger> foo(10, MyInteger(37));
(This only requires a copy constructor.) The second argument is an initializer for the elements of the vector.
In your case, you could also write:
vector<MyInteger> foo(10, 37);
...since MyInteger has a non-explicit constructor taking "int" as argument. So the compiler will cast 37 to MyInteger(37) and give the same result as example 2.
You might want to study the documentation on std::vector.
Solution 2
vector<MyInteger> foo(10, MyInteger(MY_INT_VALUE));
MyFunClass(int size, int myIntegerValue) : myVector(size, MyInteger(myIntegerValue)) {};
Solution 3
Besides all answers which answered the question very well, in a case that your class MyInteger is not copy-constructible, you could use this trick : instead of creating vector< MyInteger>
, you could create vector< shared_ptr< MyInteger > >
Solution 4
Initialization lists can be used without reference to the underlying objects.
#include <string>
#include <vector>
using namespace std;
class Test
{
public:
struct NumStr
{
int num;
string str;
};
Test(vector<int> v1,vector<NumStr> v2) : _v1(v1),_v2(v2) {}
vector<int> _v1;
vector<NumStr> _v2;
};
int main()
{
Test t={ {1,2,3}, {{1,"one"}, {2,"two"}, {3,"three"}} };
cout << t._v1[1] << " " << t._v2[1].num << " " << t._v2[1].str << endl;
return 0;
}
output: 2 2 two
Rafael S. Calsaverini
Physicist, interested in agent-based simulation and statistical physics of models inspired by economic and biological reasoning. Programming in C, C++, Haskell and Python.
Updated on July 18, 2022Comments
-
Rafael S. Calsaverini almost 2 years
suppose I have the following class:
class MyInteger { private: int n_; public: MyInteger(int n) : n_(n) {}; // MORE STUFF };
And suppose this class don't have a default trivial constructor
MyInteger()
. I must always supply anint
to initialize it for some reason. And then suppose that somewhere in my code I need avector<MyInteger>
. How do I initialize eachMyInteger
component in thisvector<>
?I have two situations (probably the solution is the same, but I'll state them anyway), a normal variable inside a function:
int main(){ vector<MyInteger> foo(10); //how do I initialize each //MyInteger field of this vector? doStuff(foo); }
and as data in a class:
class MyFunClass { private: vector<MyInteger> myVector; public: MyFunClass(int size, int myIntegerValue) : myVector(size) {}; // what do I put here if I need the // initialization to call MyInteger(myIntegerValue) for all // components of myVector? };
Is it possible to do it just in the initialization list or must I write the initialization by hand in the MyFunClass(int, int) constructor?
This seems so very basic, and yet I somehow missed it inmy book and can't find in the web.