mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-02-20 15:14:26 +01:00
Swap and simple sort for the vector.
This commit is contained in:
parent
43b7a352a5
commit
212a144215
@ -17,6 +17,10 @@ public:
|
|||||||
T get(const int index);
|
T get(const int index);
|
||||||
const T &get(const int index) const;
|
const T &get(const int index) const;
|
||||||
void set(const int index, const T &value);
|
void set(const int index, const T &value);
|
||||||
|
void swap(const int index1, const int index2);
|
||||||
|
|
||||||
|
void sort_inc();
|
||||||
|
void sort_dec();
|
||||||
|
|
||||||
int size() const;
|
int size() const;
|
||||||
int capacity() const;
|
int capacity() const;
|
||||||
@ -109,6 +113,35 @@ void Vector<T>::set(const int index, const T &value) {
|
|||||||
_data[index] = value;
|
_data[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Vector<T>::swap(const int index1, const int index2) {
|
||||||
|
T e = _data[index1];
|
||||||
|
_data[index1] = _data[index2];
|
||||||
|
_data[index2] = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Vector<T>::sort_inc() {
|
||||||
|
for (int i = 0; i < _size; ++i) {
|
||||||
|
for (int j = i + 1; j < _size; ++j) {
|
||||||
|
if (_data[j] < _data[i]) {
|
||||||
|
swap(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void Vector<T>::sort_dec() {
|
||||||
|
for (int i = 0; i < _size; ++i) {
|
||||||
|
for (int j = i + 1; j < _size; ++j) {
|
||||||
|
if (_data[j] > _data[i]) {
|
||||||
|
swap(i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
int Vector<T>::size() const {
|
int Vector<T>::size() const {
|
||||||
return _size;
|
return _size;
|
||||||
|
Loading…
Reference in New Issue
Block a user