Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Vector impl
//Title of this code #include <iostream> using namespace std; template<class T> class MyVector { T* container; int size; void resize(int); inline T& getElementRef(int); public: MyVector(int); ~MyVector(); T& at(int); T& operator[](int); int getSize(); }; template<class T> void MyVector<T>::resize(int pos) { while (size <= pos) size *= 2; container = static_cast<T*>(realloc(container, size)); } template<class T> inline T& MyVector<T>::getElementRef(int pos) { if (pos >= size) resize(pos); return container[pos]; } template<class T> MyVector<T>::MyVector(int size) { this->size = size; container = static_cast<T*>(malloc(size * sizeof(T))); } template<class T> MyVector<T>::~MyVector() { free(container); } template<class T> T& MyVector<T>::at(int pos) { return getElementRef(pos); } template<class T> T& MyVector<T>::operator[](int pos) { return getElementRef(pos); } template<class T> int MyVector<T>::getSize() { return size; } int main() { MyVector<int> v(20); v[0] = 10; v.at(5000) = 20; cout << v[0] << " " << v.at(5000) << " " << v.getSize() << endl; }
run
|
edit
|
history
|
help
0
project: bank account
qsort
Backpack with recursion
3
VirtualResto
PalindromeDay
PhoneDirectory
string match2
Base conversion
PriorQ2