Skip to content

Instantly share code, notes, and snippets.

@BalintCsala
Created April 19, 2020 19:26
Show Gist options
  • Save BalintCsala/332709d21672524f1ed9c2e6687286a3 to your computer and use it in GitHub Desktop.
Save BalintCsala/332709d21672524f1ed9c2e6687286a3 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
template <typename T>
void quicksort(T *eleje, T *vege) {
if (eleje == vege)
return;
T *kisebb = new T[vege - eleje];
T *nagyobb = new T[vege - eleje];
int kisebbIndex = 0;
int nagyobbIndex = 1;
nagyobb[0] = *eleje;
for (T *mozgo = eleje + 1; mozgo < vege; mozgo++) {
if (*mozgo < *eleje) {
kisebb[kisebbIndex++] = *mozgo;
} else {
nagyobb[nagyobbIndex++] = *mozgo;
}
}
for (int i = 0; i < kisebbIndex; i++) {
*(eleje + i) = kisebb[kisebbIndex];
}
for (int i = 0; i < nagyobbIndex; i++) {
*(eleje + kisebbIndex + i) = nagyobb[nagyobbIndex];
}
quicksort(eleje, eleje + kisebbIndex);
quicksort(eleje + kisebbIndex + 1, vege);
}
class Animal {
public:
virtual void makeSound() {
std::cout << "Semmi" << std::endl;
}
};
class Lion : public Animal {
public:
virtual void makeSound() {
std::cout << "Raawr!" << std::endl;
}
};
class Monkey : public Animal {
public:
virtual void makeSound() {
std::cout << "UaUaUUa!" << std::endl;
}
};
class Human : public Monkey {
public:
virtual void makeSound() {
std::cout << "uauauauua!" << std::endl;
}
};
class Rami : public Human {
public:
virtual void makeSound() {
std::cout << "Awwwww" << std::endl;
}
};
template <typename T>
class vector {
T **arr;
size_t cap;
size_t s;
public:
vector()
:s(0), cap(0), arr(new T*[0])
{}
void add(T &obj) {
if (s >= cap) {
cap = cap * 2 + 1;
T **uj = new T*[cap];
for (int i = 0; i < s; i++) {
uj[i] = arr[i];
}
delete[] arr;
arr = uj;
}
arr[s++] = &obj;
}
size_t capacity() {
return cap;
}
size_t size() {
return s;
}
T &operator[](int i) {
if (i < 0 || i > s)
throw std::invalid_argument("Invalid index!");
return *arr[i];
}
};
int main() {
srand(time(NULL));
int *arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = rand() % 100;
}
quicksort(arr, arr + 10);
for (int i = 0; i < 10; i++) {
std::cout << arr[i] << std::endl;
}
delete arr;
Monkey monkey;
Lion lion;
Rami rami;
Human human;
vector<Animal> vector;
vector.add(monkey);
vector.add(lion);
vector.add(rami);
vector.add(human);
std::cout << "Size: " << vector.size() << ", Capacity: " << vector.capacity() << std::endl;
for (size_t i = 0; i < vector.size(); i++) {
vector[i].makeSound();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment