vector intern
-
wo finde ich, wie vector von STL intern genau arbeitet?
-
µ$µ schrieb:
wo finde ich, wie vector von STL intern genau arbeitet?
Im Quellcode. Die vector include ist ein guter Anfang.
-
z.B. auch durch eigene Experimente mit einer eigenen "Verräterklasse":
#define _TEST_ #include <windows.h> #include <conio.h> #include <iostream> /* 0 BLACK, 1 BLUE, 2 GREEN, 3 CYAN, 4 RED, 5 MAGENTA, 6 BROWN, 7 LIGHTGRAY, 8 DARKGRAY, 9 LIGHTBLUE, 10 LIGHTGREEN, 11 LIGHTCYAN, 12 LIGHTRED, 13 LIGHTMAGENTA, 14 YELLOW, 15 WHITE */ void textcolor(WORD color) { SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color); } const int farbe1 = 3; const int farbe2 = 15; class Xint { private: int num; static int countCtor; static int countDtor; static int countCopycon; static int countOpAssign; public: Xint() { #ifdef _TEST_ textcolor(farbe1); std::cout << this << ": " << "ctor" << std::endl; textcolor(farbe1); #endif ++countCtor; } ~Xint() { #ifdef _TEST_ textcolor(farbe1); std::cout << this << ": " << "dtor" << std::endl; textcolor(farbe2); #endif ++countDtor; } Xint(const Xint& x) { #ifdef _TEST_ textcolor(farbe1); std::cout << this << ": " << "copycon von " << std::dec << &x << std::endl; textcolor(farbe2); #endif num = x.getNum(); ++countCopycon; } Xint& operator=(const Xint& x) { if (&x == this) { #ifdef _TEST_ textcolor(farbe1); std::cout << "Selbstzuweisung mit op=" << std::endl; textcolor(farbe2); #endif } #ifdef _TEST_ textcolor(farbe1); std::cout << this << ": " << "op= von " << std::dec << &x << std::endl; textcolor(farbe2); #endif num = x.getNum(); ++countOpAssign; return *this; } int getNum() const {return num;} void setNum(int val) {num = val;} static void statistik(std::ostream&); static void reset(); }; int Xint::countCtor = 0; int Xint::countDtor = 0; int Xint::countCopycon = 0; int Xint::countOpAssign = 0; void Xint::statistik(std::ostream& os) { textcolor(farbe1); os << "Ctor: " << countCtor << std::endl << "Dtor: " << countDtor << std::endl << "Copycon: " << countCopycon << std::endl << "op=: " << countOpAssign; textcolor(farbe2); } void Xint::reset() { countCtor = 0; countDtor = 0; countCopycon = 0; countOpAssign = 0; } std::ostream& operator<< (std::ostream& os, const Xint& x) { os << x.getNum(); return os; } bool operator< (const Xint& a, const Xint& b) { return a.getNum() < b.getNum(); } bool operator> (const Xint& a, const Xint& b) { return a.getNum() > b.getNum(); } bool operator== (const Xint& a, const Xint& b) { return a.getNum() == b.getNum(); } bool operator!= (const Xint& a, const Xint& b) { return a.getNum() != b.getNum(); } /***********************************************************/ #include <vector> using namespace std; int main() { const char NL = '\n'; const int N = 2; X x; cout << NL; vector<X> v; cout << "Size: " << v.size() << NL; cout << "Capacity: " << v.capacity() << NL; cout << NL; v.reserve(N); cout << "Size: " << v.size() << NL; cout << "Capacity: " << v.capacity() << NL; cout << NL; for(size_t i=0; i<N; ++i) v.push_back(x); cout << NL; cout << "Size: " << v.size() << NL; cout << "Capacity: " << v.capacity() << NL; for(size_t i=0; i<v.capacity(); ++i) cout << &v[i] << ": " << v[i].getNum() << NL; cout << NL; x.setNum(333); for(size_t i=0; i<N; ++i) v[i]=x; cout << NL; cout << "Size: " << v.size() << NL; cout << "Capacity: " << v.capacity() << NL; for(size_t i=0; i<v.capacity(); ++i) cout << &v[i] << ": " << v[i].getNum() << NL; cout << NL; vector<X>::iterator it = v.begin()+1; x.setNum(911); v.insert(it,x); cout << NL; cout << "Size: " << v.size() << NL; cout << "Capacity: " << v.capacity() << NL; for(size_t i=0; i<v.capacity(); ++i) cout << &v[i] << ": " << v[i].getNum() << NL; cin.get(); }
0x22ff58: ctor Size: 0 Capacity: 0 Size: 0 Capacity: 2 0x3f2528: copycon von 0x22ff58 0x3f252c: copycon von 0x22ff58 Size: 2 Capacity: 2 0x3f2528: 42 0x3f252c: 42 0x3f2528: op= von 0x22ff58 0x3f252c: op= von 0x22ff58 Size: 2 Capacity: 2 0x3f2528: 333 0x3f252c: 333 0x3f25c8: copycon von 0x3f2528 0x3f25cc: copycon von 0x22ff58 0x3f25d0: copycon von 0x3f252c 0x3f2528: dtor 0x3f252c: dtor Size: 3 Capacity: 4 0x3f25c8: 333 0x3f25cc: 911 0x3f25d0: 333 0x3f25d4: 1142973805