how to create a vector of a class object ?

Feb 19, 2009 at 6:49pm
i need to create a vector of a class object, for example n copies of an object a.
please guide.
Feb 19, 2009 at 7:08pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
using std::vector;

class MyClass { .... };

vector<MyClass> myVector( 100, MyClass() );
/* myVector now contains 100 objects of type MyClass, built with the default constructor */

vector<MyClass> myVector2;
// MyVector2 is empty

myVector2.assign( 100, MyClass() );

// My Vector2 now contains 100 objects of type MyClass, built with default ctor


There's nothing here you couldn't learn from http://www.cplusplus.com/reference/stl/vector/
Feb 20, 2009 at 6:24pm
Thanks a lot nonzenze.
Topic archived. No new replies allowed.