Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Vector impl
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.31 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.46 sec
edit mode
|
history
|
discussion
10 20 5120