mirror of
https://github.com/Relintai/programming_tutorials.git
synced 2025-04-21 21:51:22 +02:00
IntVector, Vector.
This commit is contained in:
parent
7f80e0d3d8
commit
6e358e75d3
6
.vscode/settings.json
vendored
6
.vscode/settings.json
vendored
@ -51,6 +51,10 @@
|
||||
"stdexcept": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"variant": "cpp"
|
||||
"variant": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"bitset": "cpp",
|
||||
"valarray": "cpp"
|
||||
}
|
||||
}
|
@ -3,10 +3,11 @@ rev 2: abs() képlet hozzáadva.
|
||||
rev 3: Hozzáadtam vector és float-os közötti szorzás operátorokat mindkét vektorhoz.
|
||||
rev 4: Vector length képlet hozzáadva.
|
||||
rev 5: Vector dot() képletben hiányzott egy .y
|
||||
rev 6: lenght_squared -> length_squared
|
||||
rev 6: lenght_squared -> length_squared.
|
||||
rev 7: Rect2 is_equal_approx paraméter típus kijavítva.
|
||||
rev 8: Rect2 grow, shrink visszatérési érték javítva (Már nincs)
|
||||
rev 9: intersects_include_borders typo javítva
|
||||
rev 8: Rect2 grow, shrink visszatérési érték javítva (Már nincs).
|
||||
rev 9: intersects_include_borders typo javítva.
|
||||
rev 10: IntVectorba pop_back metódus hozzáadva.
|
||||
|
||||
1. Implementáld az alábbi 2ds matematikai vektor osztályt:
|
||||
|
||||
@ -169,6 +170,7 @@ return (b.x >= x) && (b.y >= y) &&
|
||||
| class IntVector |
|
||||
|-----------------------------------------------------|
|
||||
| + push_back(element : int) |
|
||||
| + pop_back() | -> utolsó elem törlése
|
||||
| + remove(index : int) |
|
||||
| + erase(element : int) |
|
||||
| + clear() |
|
||||
@ -199,6 +201,9 @@ return (b.x >= x) && (b.y >= y) &&
|
||||
|
||||
5. A 4. feladatban elékszített IntVektor osztályból csinálj egy templatekkel általánosított Vector osztályt.
|
||||
|
||||
Fontos! A templatelt tagfüggvényeket implementációját vector.h-ba kell rakni! Vagy bele az osztályba, vagy a vector.h aljára!
|
||||
Enélkül undefined reference hibákat fog dobmi a fordító!
|
||||
|
||||
Eddíg pl:
|
||||
|
||||
class IntVector {
|
||||
|
@ -7,9 +7,12 @@ if [ ! -d "bin" ]; then
|
||||
mkdir bin
|
||||
fi
|
||||
|
||||
g++ -Wall -g -Iinclude -c main.cpp -o obj/main.o
|
||||
g++ -Wall -g -Iinclude -c vector2.cpp -o obj/vector2.o
|
||||
g++ -Wall -g -Iinclude -c vector3.cpp -o obj/vector3.o
|
||||
g++ -Wall -g -Iinclude -c rect2.cpp -o obj/rect2.o
|
||||
g++ -o bin/program obj/main.o obj/vector2.o obj/vector3.o obj/rect2.o
|
||||
#-Iinclude
|
||||
|
||||
g++ -Wall -g -c vector2.cpp -o obj/vector2.o
|
||||
g++ -Wall -g -c vector3.cpp -o obj/vector3.o
|
||||
g++ -Wall -g -c rect2.cpp -o obj/rect2.o
|
||||
g++ -Wall -g -c int_vector.cpp -o obj/int_vector.o
|
||||
g++ -Wall -g -c main.cpp -o obj/main.o
|
||||
g++ -o bin/program obj/vector2.o obj/vector3.o obj/rect2.o obj/int_vector.o obj/main.o
|
||||
|
||||
|
140
01_alapok/int_vector.cpp
Normal file
140
01_alapok/int_vector.cpp
Normal file
@ -0,0 +1,140 @@
|
||||
#include "int_vector.h"
|
||||
|
||||
void IntVector::push_back(const int element) {
|
||||
ensure_capacity(_size + 1);
|
||||
|
||||
_data[_size++] = element;
|
||||
}
|
||||
|
||||
void IntVector::pop_back() {
|
||||
if (_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
void IntVector::remove(const int index) {
|
||||
_data[index] = _data[_size - 1];
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
void IntVector::erase(const int element) {
|
||||
int index = find(element);
|
||||
|
||||
if (index != -1) {
|
||||
remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
void IntVector::clear() {
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
bool IntVector::empty() const {
|
||||
return _size == 0;
|
||||
}
|
||||
|
||||
int IntVector::get(const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
const int &IntVector::get(const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
void IntVector::set(const int index, const int value) {
|
||||
_data[index] = value;
|
||||
}
|
||||
|
||||
int IntVector::size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
int IntVector::capacity() const {
|
||||
return _actual_size;
|
||||
}
|
||||
|
||||
void IntVector::ensure_capacity(const int capacity) {
|
||||
if (capacity <= _actual_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
int tsize = capacity + _grow_by;
|
||||
|
||||
int *nd = new int[tsize];
|
||||
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
nd[i] = _data[i];
|
||||
}
|
||||
|
||||
delete[] _data;
|
||||
|
||||
_data = nd;
|
||||
}
|
||||
|
||||
void IntVector::resize(const int s) {
|
||||
ensure_capacity(s);
|
||||
|
||||
_size = s;
|
||||
}
|
||||
|
||||
void IntVector::append_array(const IntVector &other) {
|
||||
ensure_capacity(_size + other._size);
|
||||
|
||||
for (int i = 0; i < other._size; ++i) {
|
||||
_data[_size++] = other._data[i];
|
||||
}
|
||||
}
|
||||
|
||||
int IntVector::find(const int val) const {
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
if (_data[i] == val) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int *IntVector::dataw() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
const int *IntVector::data() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
const int &IntVector::operator[](const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
int &IntVector::operator[](const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
IntVector::IntVector() {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
}
|
||||
|
||||
IntVector::IntVector(int prealloc) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
||||
|
||||
IntVector::IntVector(int prealloc, int grow_by) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = grow_by;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
41
01_alapok/int_vector.h
Normal file
41
01_alapok/int_vector.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef IntVector_H
|
||||
#define IntVector_H
|
||||
|
||||
class IntVector {
|
||||
|
||||
public:
|
||||
void push_back(const int element);
|
||||
void pop_back();
|
||||
void remove(const int index);
|
||||
void erase(const int element);
|
||||
void clear();
|
||||
bool empty() const;
|
||||
int get(const int index);
|
||||
const int &get(const int index) const;
|
||||
void set(const int index, const int value);
|
||||
|
||||
int size() const;
|
||||
int capacity() const;
|
||||
void ensure_capacity(const int capacity);
|
||||
void resize(const int s);
|
||||
void append_array(const IntVector &other);
|
||||
int find(const int val) const;
|
||||
|
||||
int *dataw();
|
||||
const int *data() const;
|
||||
|
||||
const int &operator[](const int index) const;
|
||||
int &operator[](const int index);
|
||||
|
||||
IntVector();
|
||||
IntVector(int prealloc);
|
||||
IntVector(int prealloc, int grow_by);
|
||||
|
||||
private:
|
||||
int *_data;
|
||||
int _actual_size;
|
||||
int _size;
|
||||
int _grow_by;
|
||||
};
|
||||
|
||||
#endif
|
@ -1,7 +1,35 @@
|
||||
|
||||
#include<iostream>
|
||||
#include <iostream>
|
||||
|
||||
#include "int_vector.h"
|
||||
#include "vector.h"
|
||||
|
||||
int main() {
|
||||
|
||||
//iv
|
||||
IntVector iv;
|
||||
|
||||
iv.push_back(123);
|
||||
iv.push_back(232);
|
||||
|
||||
for (int i = 0; i < iv.size(); ++i) {
|
||||
std::cout << iv[i] << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
//v
|
||||
Vector<int> v;
|
||||
|
||||
v.push_back(123);
|
||||
v.push_back(232);
|
||||
|
||||
for (int i = 0; i < v.size(); ++i) {
|
||||
std::cout << v[i] << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
std::cout << "asd" << std::endl;
|
||||
return 0;
|
||||
}
|
203
01_alapok/vector.h
Normal file
203
01_alapok/vector.h
Normal file
@ -0,0 +1,203 @@
|
||||
#ifndef VECTOR_H
|
||||
#define VECTOR_H
|
||||
|
||||
template <class T>
|
||||
class Vector {
|
||||
|
||||
public:
|
||||
void push_back(const T &element);
|
||||
void pop_back();
|
||||
void remove(const int index);
|
||||
void erase(const T &element);
|
||||
void clear();
|
||||
bool empty() const;
|
||||
T get(const int index);
|
||||
const T &get(const int index) const;
|
||||
void set(const int index, const T &value);
|
||||
|
||||
int size() const;
|
||||
int capacity() const;
|
||||
void ensure_capacity(const int capacity);
|
||||
void resize(const int s);
|
||||
void append_array(const Vector<T> &other);
|
||||
int find(const T &val) const;
|
||||
|
||||
int *dataw();
|
||||
const int *data() const;
|
||||
|
||||
const T &operator[](const int index) const;
|
||||
T &operator[](const int index);
|
||||
|
||||
Vector();
|
||||
Vector(int prealloc);
|
||||
Vector(int prealloc, int grow_by);
|
||||
|
||||
private:
|
||||
T *_data;
|
||||
int _actual_size;
|
||||
int _size;
|
||||
int _grow_by;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::push_back(const T &element) {
|
||||
ensure_capacity(_size + 1);
|
||||
|
||||
_data[_size++] = element;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::pop_back() {
|
||||
if (_size == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::remove(const int index) {
|
||||
_data[index] = _data[_size - 1];
|
||||
|
||||
--_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::erase(const T &element) {
|
||||
int index = find(element);
|
||||
|
||||
if (index != -1) {
|
||||
remove(index);
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::clear() {
|
||||
_size = 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
bool Vector<T>::empty() const {
|
||||
return _size == 0;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T Vector<T>::get(const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T &Vector<T>::get(const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::set(const int index, const T &value) {
|
||||
_data[index] = value;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::capacity() const {
|
||||
return _actual_size;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::ensure_capacity(const int capacity) {
|
||||
if (capacity <= _actual_size) {
|
||||
return;
|
||||
}
|
||||
|
||||
int tsize = capacity + _grow_by;
|
||||
|
||||
T *nd = new T[tsize];
|
||||
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
nd[i] = _data[i];
|
||||
}
|
||||
|
||||
delete[] _data;
|
||||
|
||||
_data = nd;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::resize(const int s) {
|
||||
ensure_capacity(s);
|
||||
|
||||
_size = s;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void Vector<T>::append_array(const Vector<T> &other) {
|
||||
ensure_capacity(_size + other._size);
|
||||
|
||||
for (int i = 0; i < other._size; ++i) {
|
||||
_data[_size++] = other._data[i];
|
||||
}
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int Vector<T>::find(const T &val) const {
|
||||
for (int i = 0; i < _size; ++i) {
|
||||
if (_data[i] == val) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
int *Vector<T>::dataw() {
|
||||
return _data;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const int *Vector<T>::data() const {
|
||||
return _data;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
const T &Vector<T>::operator[](const int index) const {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
T &Vector<T>::operator[](const int index) {
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector() {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector(int prealloc) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = 100;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
Vector<T>::Vector(int prealloc, int grow_by) {
|
||||
_data = nullptr;
|
||||
_actual_size = 0;
|
||||
_size = 0;
|
||||
_grow_by = grow_by;
|
||||
|
||||
ensure_capacity(prealloc);
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user