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

edit

parent 216533f9
No related branches found
No related tags found
No related merge requests found
^C:\USERS\TOMAS\DOCUMENTS\SKOLA\VS\SEMESTR_2\PPC\PPC\CVICENI\CV04\BUILD\CMAKEFILES\0201A46DFD6067B0A1C13457B1922A89\GENERATE.STAMP.RULE
C:\USERS\TOMAS\DOCUMENTS\SKOLA\VS\SEMESTR_2\PPC\PPC\CVICENI\CV04\BUILD\CMAKEFILES\GENERATE.STAMP
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.43.34808:TargetPlatformVersion=10.0.22621.0:
Debug|x64|C:\Users\tomas\Documents\skola\VS\Semestr_2\PPC\ppc\cviceni\cv04\build\|
#include <iostream>
#include <myvector.hpp>
#include <numeric>
#include <numeric> // std::accumulate
#include <algorithm>
#include "myvector.hpp"
int multiply(int &a, int &b)
{
return a*b;
}
std::ostream& operator<<(std::ostream& os, MyVector& v)
{
for(int idx=0; idx<v.size; idx++){
if(idx){
os<<", ";
}
os<<v.parr[idx];
for(int idx = 0; idx < v.size; ++idx)
{
if(idx)
{
os << ", ";
}
return os;
os << v.parr[idx];
}
return os;
}
int main(void)
{
MyVector v;
MyVector v, u;
for(int idx=0; idx<10; idx++)
{
v.push_back(idx+1);
}
std::cout << "v.Size() = " << v.Size() << std::endl;
for(int idx = 0; idx < 10; ++idx)
{
v.push_back(idx + 1);
std::cout << v[idx] << ", ";
u.push_back((10 - idx)*5);
}
std::cout << std::endl;
std::cout << "v.Size() = " << v.Size() << std::endl;
std::cout<<std::endl;
std::cout << "Size: " << v.Size() << std::endl;
std::cout << "v = " << v << std::endl;
std::cout << "u = " << u << std::endl;
std::cout << v << std::endl;
std::cout << "Foreach" << std::endl;
//for(int a : v)
for(MyVector::Iterator it = v.begin(); it != v.end(); ++it)
{
int a = *it;
std::cout << a << " ";
}
std::cout << std::endl;
std::cout<<"Foreach"<<std::endl;
for(int a:v){
std::cout<<a<<", ";
}
std::cout << v << std::endl;
std::cout << "Accumulate(v) = ";
std::cout << std::accumulate(v.begin(), v.end(), 0);
std::cout << std::endl;
std::cout << "Accumulate-multiply(v) = ";
std::cout << std::accumulate(v.begin(), v.end(), 1, multiply) << std::endl;
std::cout<<"Acumulate = ";
std::cout<<std::accumulate(v.begin(), v.end(), 0);
std::cout<<std::endl;
// sort s takto jednoduchym iteratorem nejde
// viz. https://stackoverflow.com/questions/55940777/custom-iterator-for-use-in-stdsort
// std::sort(v.begin(), v.end());
u.push_back(v);
std::cout << "u join v = " << u << std::endl;
return 0;
}
return 0;
}
\ No newline at end of file
#include "myvector.hpp"
#include <iostream>
#include <new>
//#include <stdexcept>
//
MyVector::MyVector(int size)
// vytvor vektor dane velikosti
MyVector::MyVector(size_t size)
{
resize(size);
// inicializovat/alokovat pole na velikost size
// inicializovat promennou size
resize(size);
}
MyVector::~MyVector()
{
delete[] parr;
delete[] parr;
}
void MyVector::resize(int newsize)
void MyVector::resize(size_t newsize)
{
int *tmp = NULL;
try
{
{
tmp = new int[newsize];
}
}
catch (const std::bad_alloc e)
{
std::cout << "Chyba alokace: " << e.what() << '\n';
}
// Zmen velikost pole naseho vektoru
int *tmp = NULL;
try
{
tmp = new int[newsize];
}
catch(std::bad_alloc e)
{
std::cerr << "Alokace se nezdarila: " << e.what()
<< std::endl;
}
int(tmp != NULL)
{
if (parr != NULL)
{
std::copy(&pat[0], &parr[size], tmp) delete[] parr;
}
parr = tmp;
size = newsize;
if(tmp != NULL) // povedla se alokace?
{
if(parr != NULL) // mame neco v poli?
{ // Prekopiruj data od zacatku do konce pole do tmp
std::copy(&parr[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
}
resize(size + 1); // vytvor misto pro novou hodnotu
parr[size - 1] = value; // uloz na nove misto
}
int MyVector::operator[](int idx)
void MyVector::push_back(MyVector& u)
{
if(idx>=size){
throw std::out_of_range("Index mimo rozsah pole");
}
}
\ No newline at end of file
size_t oldsize = size;
resize(size + u.size); // vytvor misto pro nove hodnoty
std::copy(&u.parr[0], &u.parr[u.size], &parr[oldsize]); // uloz nove hodnoty
}
int MyVector::operator[](size_t idx)
{
if(idx >= size)
{
throw std::out_of_range("Index mimo rozsah");
}
return parr[idx];
}
......@@ -4,49 +4,55 @@
#define MYVECTOR_HPP_HDR
#include <cstddef>
#include <stdexcept>
#include <iostream>
class MyVector
{
private:
private:
int *parr;
size_t size;
void resize(size_t newSize);
public:
MyVector() : size(0), parr(NULL) {};
MyVector(int size);
void resize(size_t newsize);
public:
MyVector() : parr(NULL), size(0) {};
MyVector(size_t 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;}
};
size_t Size() const { return size; }
void push_back(int value); // Pridej jednu hodnotu
void push_back(MyVector& u); // Pridej cely vektor
// Pretizene operatory
int operator[](size_t idx); // pouze pro cteni
//int& operator[](size_t idx); // s referenci umoznuje i zapis
// Musi byt friend a neclenska, protoze ji musime zavolat bez vazby na objekt
friend std::ostream& operator<<(std::ostream& os,
MyVector& v);
class Iterator
{
int *ptr;
public:
Iterator(int* p) : ptr(p) {};
int& operator*(void) const { return *ptr; }
Iterator& operator++(void)
{
ptr++;
return *this;
}
bool operator!=(const Iterator& b) { return ptr != b.ptr; }
};
Iterator begin() { return Iterator(&parr[0]); }
Iterator end() { return Iterator(&parr[size]); }
// Konstantni iterator
const Iterator cbegin() { return Iterator(&parr[0]); }
const Iterator cend() { return Iterator(&parr[size]); }
};
Iterator begin(){return Iterator(&parr[0]);}
Iterator end(){return Iterator(&parr[size]);}
};
#endif
\ No newline at end of file
#endif
#include <iostream>
int main(){
config.min=-99
config.max=
config.width=
config.align=
}
\ 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