Add a constructor to Vector.h. Test class GraphMatrix, all tests passed. Dfs and Bfs remained to be done.
This commit is contained in:
@@ -34,6 +34,7 @@ public:
|
||||
//constructors
|
||||
Vector();
|
||||
Vector(int capacity);
|
||||
Vector(int size, T const &value);
|
||||
Vector(T* const A, int n) { copyfrom(A, 0, n); }
|
||||
Vector(T* const A, int lo, int hi) { copyfrom(A, lo, hi); }
|
||||
Vector(Vector<T> const &V) { copyfrom(V._elem, 0, V._size); }
|
||||
@@ -149,6 +150,15 @@ Vector<T>::Vector(int capacity) {
|
||||
_elem = new T[_capacity];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vector<T>::Vector(int size, T const &value){
|
||||
_capacity = size << 1;
|
||||
_size = size;
|
||||
_elem = new T[_capacity];
|
||||
for (int ix = 0; ix != _size; ++ix)
|
||||
_elem[ix] = value;
|
||||
}
|
||||
|
||||
//read-only interfaces
|
||||
template<typename T>
|
||||
void Vector<T>::print() {
|
||||
|
||||
@@ -66,6 +66,12 @@ void test_constructor() {
|
||||
assert(V5.getSize() == 2);
|
||||
assert(V5.getCapacity() == 4);
|
||||
|
||||
Vector<int> V6(10, 0);
|
||||
assert(V6.getSize() == 10);
|
||||
assert(V6.getCapacity() == 20);
|
||||
for (int ix = 0; ix != 10; ++ix)
|
||||
assert(V6[ix] == 0);
|
||||
|
||||
cout << "test passed." << endl;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user