// (C) Porkolab 2003 // // A.6.4. // Constructors template <class T, class A = allocator<T> > class vector { public: // ... // constructors, etc.: explicit vector(const A& = A()); explicit vector(size_type n, const T& val = T(), const A& = A()); // n copies of val template <class In> // In must be an input iterator vector(In first, In last, const A& = A()); // copy from [first:last[ vector(const vector& x); ~vector(); // destructor vector& operator=(const vector& x); // assignment operator template <class In> // In must be an input iterator (_iter.oper_) void assign(In first, In last); // copy from [first:last[ void assign(size_type n, const T& val); // n copies of val // ... }; /* * Use of constructors * */ vector<Record> vr(10000); void f(int s1, int s2) { vector<int> vi(s1); vector<double>* p = new vector<double>(s2); } void f(const list<X>& lst) { vector<X> v1(lst.begin(),lst.end()); // copy elements from list char p[] = "despair"; vector<char> v2(p,&p[sizeof(p)-1]); // copy characters from C-style string } vector<int> v1(10); // ok: vector of 10 ints vector<int> v2 = vector<int>(10); // ok: vector of 10 ints vector<int> v3 = v2; // ok: v3 is a copy of v2 vector<int> v4 = 10; // error: attempted implicit conversion of 10 to vector<int> void f1(vector<int>&); // common style void f2(const vector<int>&);// common style void f3(vector<int>); // rare style void h() { vector<int> v(10000); // ... f1(v); // pass a reference f2(v); // pass a reference f3(v); // copy the 10000 elements into a new vector for f3() to use }