There are many ways to get there. Here are some of them (in no particular order of presence).
Use vector(size_type n, const T& t)
constructor. It initializes vector with n
copies of t
. For example:
#include <vector>
struct MyInt
{
int value;
MyInt (int value) : value (value) {}
};
struct MyStuff
{
std::vector<MyInt> values;
MyStuff () : values (10, MyInt (20))
{
}
};
Push elements into vector one by one. This might be useful when values should be different. For example:
#include <vector>
struct MyInt
{
int value;
MyInt (int value) : value (value) {}
};
struct MyStuff
{
std::vector<MyInt> values;
MyStuff () : values ()
{
values.reserve (10); // Reserve memory not to allocate it 10 times...
for (int i = 0; i < 10; ++i)
{
values.push_back (MyInt (i));
}
}
};
Another option is constructor initialization list, if C++0x is an option:
#include <vector>
struct MyInt
{
int value;
MyInt (int value) : value (value) {}
};
struct MyStuff
{
std::vector<MyInt> values;
MyStuff () : values ({ MyInt (1), MyInt (2), MyInt (3) /* ... */})
{
}
};
Of course, there is an option to provide default constructor and/or use something other than std::vector
.
Hope it helps.
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.
vector<MyInteger> foo(10, MyInteger(MY_INT_VALUE));
MyFunClass(int size, int myIntegerValue) : myVector(size, MyInteger(myIntegerValue)) {};
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 > >
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With