class vec4{
public:
double x,y,z,w;
};
class mat4{
public:
vec4 m_data[4];
};
C/C++
Создать два класса вектор (double *) и матрица (double **).
#include <iostream>
#include <cstring>
using namespace std;
class VectorDouble {
public:
VectorDouble() : length(0), buffer(limit), vec(new double[buffer]) {}
VectorDouble(const size_t length) : length(length), buffer(length), vec(new double[buffer]) {
memset(vec, 0, length * sizeof(double));
}
VectorDouble(initializer_list<double> lst) : length(lst.size()), buffer(length), vec(new double[buffer]) {
copy(lst.begin(), lst.end(), begin());
}
VectorDouble(const VectorDouble& vd) : length(vd.size()), buffer(vd.capacity()), vec(new double[buffer]) {
copy(vd.begin(), vd.end(), begin());
}
VectorDouble(VectorDouble&& vd) : length(vd.size()), buffer(vd.capacity()), vec(vd.vec) {
vd.length = vd.buffer = 0;
vd.vec = nullptr;
}
~VectorDouble() {
if (vec != nullptr) {
delete[] vec;
vec = nullptr;
}
}
VectorDouble& operator=(const VectorDouble& vd) {
if (this != &vd) {
delete[] this->vec;
this->length = vd.length;
this->buffer = vd.buffer;
this->vec = new double[buffer];
copy(vd.begin(), vd.end(), this->begin());
}
return *this;
}
VectorDouble& operator=(VectorDouble&& vd) {
if (this != &vd) {
delete[] this->vec;
this->length = vd.length;
this->buffer = vd.buffer;
this->vec = vd.vec;
vd.length = vd.buffer = 0;
vd.vec = nullptr;
}
return *this;
}
double& operator[](int index) {
return vec[index];
}
void push_back(const double value) {
if (length == buffer) {
auto tmp = *this;
delete[] this->vec;
this->buffer += limit;
this->vec = new double[this->buffer];
copy(tmp.begin(), tmp.end(), this->begin());
}
vec[this->length] = value;
++length;
}
void pop_back() {
if (length) --length;
}
double& at(int index) {
return vec[index];
}
const double& at(int index)const {
return vec[index];
}
double& back() {
return vec[length - 1];
}
double& front() {
return vec[0];
}
size_t size()const {
return length;
}
size_t capacity()const {
return buffer;
}
bool empty()const {
return length == 0;
}
void clear() {
length = 0;
}
void shrink_to_fit() {
auto tmp = *this;
delete[] this->vec;
this->length = this->buffer = tmp.size();
this->vec = new double[buffer];
copy(tmp.begin(), tmp.end(), this->begin());
}
double* begin()const {
return vec;
}
double* end()const {
return vec + length;
}
private:
size_t length;
size_t buffer;
double* vec;
static const auto limit = 8U;
};
int main() {
VectorDouble vd{ 1.2, 3.4, 5.6, 7.8 };
for (auto x : vd) cout << x << ' ';
puts("");
system("pause > nul");
}
#include <cstring>
using namespace std;
class VectorDouble {
public:
VectorDouble() : length(0), buffer(limit), vec(new double[buffer]) {}
VectorDouble(const size_t length) : length(length), buffer(length), vec(new double[buffer]) {
memset(vec, 0, length * sizeof(double));
}
VectorDouble(initializer_list<double> lst) : length(lst.size()), buffer(length), vec(new double[buffer]) {
copy(lst.begin(), lst.end(), begin());
}
VectorDouble(const VectorDouble& vd) : length(vd.size()), buffer(vd.capacity()), vec(new double[buffer]) {
copy(vd.begin(), vd.end(), begin());
}
VectorDouble(VectorDouble&& vd) : length(vd.size()), buffer(vd.capacity()), vec(vd.vec) {
vd.length = vd.buffer = 0;
vd.vec = nullptr;
}
~VectorDouble() {
if (vec != nullptr) {
delete[] vec;
vec = nullptr;
}
}
VectorDouble& operator=(const VectorDouble& vd) {
if (this != &vd) {
delete[] this->vec;
this->length = vd.length;
this->buffer = vd.buffer;
this->vec = new double[buffer];
copy(vd.begin(), vd.end(), this->begin());
}
return *this;
}
VectorDouble& operator=(VectorDouble&& vd) {
if (this != &vd) {
delete[] this->vec;
this->length = vd.length;
this->buffer = vd.buffer;
this->vec = vd.vec;
vd.length = vd.buffer = 0;
vd.vec = nullptr;
}
return *this;
}
double& operator[](int index) {
return vec[index];
}
void push_back(const double value) {
if (length == buffer) {
auto tmp = *this;
delete[] this->vec;
this->buffer += limit;
this->vec = new double[this->buffer];
copy(tmp.begin(), tmp.end(), this->begin());
}
vec[this->length] = value;
++length;
}
void pop_back() {
if (length) --length;
}
double& at(int index) {
return vec[index];
}
const double& at(int index)const {
return vec[index];
}
double& back() {
return vec[length - 1];
}
double& front() {
return vec[0];
}
size_t size()const {
return length;
}
size_t capacity()const {
return buffer;
}
bool empty()const {
return length == 0;
}
void clear() {
length = 0;
}
void shrink_to_fit() {
auto tmp = *this;
delete[] this->vec;
this->length = this->buffer = tmp.size();
this->vec = new double[buffer];
copy(tmp.begin(), tmp.end(), this->begin());
}
double* begin()const {
return vec;
}
double* end()const {
return vec + length;
}
private:
size_t length;
size_t buffer;
double* vec;
static const auto limit = 8U;
};
int main() {
VectorDouble vd{ 1.2, 3.4, 5.6, 7.8 };
for (auto x : vd) cout << x << ' ';
puts("");
system("pause > nul");
}
Похожие вопросы
- Создать простой класс, конструктор, и несколько функций. Не могу решить задачу, плохо понял тему, помогите пожалуйста.
- Не могу разобраться. Функция удаления отрицательных элементов вектора
- Спиральное отображения матрицы в векторе
- Поясните за float, double, long double.
- Написать программу на C++.Создать класс vector3d, задаваемый тройкой координат. Создать конструктор...
- Задание на С++ Тема: Вектор
- Как Создать иерархическую систему классов, на примере этого задания? Второй день уже туплю, помогите пожалуйста... С++
- Найти максимальный элементы в строке матрицы
- С++ Не работает двумерный вектор (std::vector)
- Чёт не пойму, как сделать рандомные значения матрицы через массив, прошу доработать, пж прогу!