Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- template<class T>
- class vec{
- private:
- T* arr = nullptr;
- int size = 0;
- int capacity = 0;
- public:
- vec(int _size_ = 0):size(_size_),capacity(_size_+20){
- if(size<0 ){
- std::cout << "Incorrect value in Cons(1)" << std::endl;
- }
- else
- arr = new T[capacity];
- }
- ~vec(){
- delete[] arr;
- }
- void push_back(T value){
- if(size == capacity){
- size++;
- capacity*=2;
- T* temp = new T[size];
- for(int i=0;i<size-1;i++){
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = new T[capacity];
- for(int i=0;i<size-1;i++){
- arr[i] = temp[i];
- }
- arr[size-1] = value;
- delete[] temp;
- }else{
- size++;
- arr[size-1] = value;
- }
- }
- T& at(int n)const{
- return arr[n];
- }
- int getsize()const{
- return size;
- }
- int getcapacity()const{
- return capacity;
- }
- void resize(int n){
- if(n < capacity){
- capacity = n;
- size = n;
- T* temp = new T[capacity];
- for(int i=0;i<n;i++){
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = new T[capacity];
- for(int i=0;i<n;i++){
- arr[i] = temp[i];
- }
- delete[] temp;
- }else if(n>capacity){
- capacity = n;
- T* temp = new T[size];
- for(int i=0;i<size;i++){
- temp[i] = arr[i];
- }
- delete[] arr;
- arr = new T[capacity];
- for(int i=0;i<size;i++){
- arr[i] = temp[i];
- }
- delete[] temp;
- }
- }
- void print()const{
- std::cout << "-------------------" << std::endl;
- for(int i=0;i<size;i++){
- std::cout << arr[i] << " ";
- }
- std::cout << std::endl;
- }
- void printBack() const {
- if(size == 0){
- std::cout <<"No elements in this vector" << std::endl;
- }else
- std::cout << arr[size-1] << std::endl;
- }
- void printFront() const {
- if(size == 0){
- std::cout << "No elements in this vector" << std::endl;
- }else
- std::cout << arr[0] << std::endl;
- }
- void erase(T value){
- if(size == 0){
- std::cout << "Cant erase from null-value array" << std::endl;
- }else{
- for(int i=0;i<size;i++){
- if(arr[i] != value){
- continue;
- }
- else{
- for(int j=i;j<size-1;j++){
- arr[j] = arr[j+1];
- }
- size--;
- return;
- }
- }
- }
- }
- void printcapacity()const{
- std::cout << capacity << std::endl;
- }
- void printsize()const{
- std::cout << size << std::endl;
- }
- void clear(){
- delete[] arr;
- capacity = 10;
- size = 0;
- arr = new T[capacity];
- }
- T& operator[](int i)const{
- return arr[i];
- }
- };
- int main(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement