c++ - default values and negative indexes when creating vector class -
i decided train myself in classes , build vecofint
class, resemble vector
container several methods.
my program works great, have found not entirely understand principles.
1. default values:
here class:
class vecofint { public: vecofint() : vec(new int[size1]) { zeros(); } ~vecofint() { delete vec; } vecofint(const int size) { set(size); vec = new int[size1]; zeros(); } vecofint& operator = (const vecofint& other) { vec = other.vec; size1 = other.size1; vec1 = other.vec1; } vecofint(const vecofint& other) : vec(other.vec), size1(other.size1), vec1(other.vec1) {} int getin(const int a) { return vec[a]; } int get() { return size1; } void set(const int a) { size1 = a; } void setin(const int a, const int val) { vec[a] = val; } int* pushback(const int val); int* pushfront(const int val); int* it; void resize(); void resize1(); void zeros() {int * = vec; fill(i, + size1, 0);} void print() { copy(vec, vec + size1, ostream_iterator<int>(cout, "\n")); } private: int size1 = 32; int* vec; int* vec1; };
as can see assign values of created vector
0. 2 reasons:
a) have found
vector <int> a(10); copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
prints 0 0 0 0 0 0 0 0 0 0
;
b) need default values implement pushback
method:
int* vecofint::pushback(const int val) { int k = 0; int* = vec; while (*it!=0) { if (k == size1) { resize(); = vec+size1/2; } ++k; ++it; } vec[k] = val; return vec; }
and use zeros. not that, because means cannot use 0
not-default values.
first question:
how can avoid these assignments zeros, still have basic values iterate through vector
? or maybe there other methods on how iterate through newly created space?
2) negative indexes
suppose trying, mistake, access negative index.
second question:
what program should write in case? error?
i have found in real vector
nothing happens, @ least until destructor called.
Comments
Post a Comment