Skip to content
Snippets Groups Projects
Commit 216533f9 authored by Tomáš Pachman's avatar Tomáš Pachman
Browse files

edit

parent 8fd9c13b
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required(VERSION 3.15)
project(cv04)
add_executable(overload main.cpp myvector.cpp)
\ No newline at end of file
#include <iostream>
#include <myvector.hpp>
#include <numeric>
std::ostream& operator<<(std::ostream& os, MyVector& v)
{
for(int idx=0; idx<v.size; idx++){
if(idx){
os<<", ";
}
os<<v.parr[idx];
}
return os;
}
int main(void)
{
MyVector v;
for(int idx=0; idx<10; idx++)
{
v.push_back(idx+1);
}
std::cout<<std::endl;
std::cout << "Size: " << v.Size() << std::endl;
std::cout << v << std::endl;
std::cout<<"Foreach"<<std::endl;
for(int a:v){
std::cout<<a<<", ";
}
std::cout << v << std::endl;
std::cout<<"Acumulate = ";
std::cout<<std::accumulate(v.begin(), v.end(), 0);
std::cout<<std::endl;
return 0;
}
\ No newline at end of file
#include "myvector.hpp"
#include <iostream>
#include <new>
//
MyVector::MyVector(int size)
{
resize(size);
}
MyVector::~MyVector()
{
delete[] parr;
}
void MyVector::resize(int newsize)
{
int *tmp = NULL;
try
{
{
tmp = new int[newsize];
}
}
catch (const std::bad_alloc e)
{
std::cout << "Chyba alokace: " << e.what() << '\n';
}
int(tmp != NULL)
{
if (parr != NULL)
{
std::copy(&pat[0], &parr[size], tmp) delete[] parr;
}
parr = tmp;
size = newsize;
}
}
void MyVector::push_back(int value)
{
resize(size + 1); //*vytvoreni mista pro novou hodnotu
parr[size - 1] = value; //*ulozeni hodnoty na konec pole
}
int MyVector::operator[](int idx)
{
if(idx>=size){
throw std::out_of_range("Index mimo rozsah pole");
}
}
\ No newline at end of file
//*do mingu dat cmake ..
//*cmake --build .
#ifndef MYVECTOR_HPP_HDR
#define MYVECTOR_HPP_HDR
#include <cstddef>
#include <stdexcept>
#include <iostream>
class MyVector
{
private:
int *parr;
size_t size;
void resize(size_t newSize);
public:
MyVector() : size(0), parr(NULL) {};
MyVector(int size);
~MyVector();
//*funkce ktera vyhodi size i kdyz je private
int Size() const { return size; }
int operator[](int idx);
void push_back(int value);
//* & je reference
//*friend udela neclenskou funkci
//*friend funkce muye k privatnim castem objektu
friend std::ostream& operator<<(std::ostream& os, MyVector& v);
class Iterator{
int* ptr;
public:
Iterator(int* p): ptr(p) {};
int& opertor*(void)const{return *ptr;}
Iterator& operator++{
ptr++;
return *this;
}
bool operator!=(const Iterartor& b){return ptr != b.ptr;}
};
Iterator begin(){return Iterator(&parr[0]);}
Iterator end(){return Iterator(&parr[size]);}
};
#endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment