TU-Programmieren_2/lab3/taskA.Vector.hpp
2025-04-09 10:22:44 +02:00

47 lines
1.6 KiB
C++

/// @file
/// @brief task A: Class Vector
#pragma once
#include <functional> // std::function
#include <iostream> // std::cout, std::endl
#include <vector> // std::vector
/// @todo implement a class 'Vector' according to the following specification:
/// - use a *private* member variable of type std::vector<double>
/// - provide 3 constructors:
/// 1. passing a std::vector<double>
/// - the values will be copied over to the private member
/// 2. passing a count 'N' and an interval specified by a 'start', and 'end' value
/// - fill the private member with 'N' equidistant samples from the interval
/// 3. passing a count 'N' and a generator function in form of a callable std::function<double()>
/// - fill the private member with 'N' samples obtained from the generator/callable
/// - member function print():
/// - prints all elements in the private member to the console
class Vector {
std::vector<double> data_;
public:
Vector(std::vector<double> data) : data_(data) {}
Vector(double start, double end, unsigned int N) {
double step = (end - start) / (N - 1);
for (unsigned int i = 0; i < N; ++i) {
data_.push_back(start + i * step);
}
}
Vector(unsigned int N, std::function<double()> generator) {
for (unsigned int i = 0; i < N; ++i) {
data_.push_back(generator());
}
}
void print() {
std::cout << "[";
for (unsigned int i = 0; i < data_.size(); ++i) {
std::cout << data_[i];
if (i < data_.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]" << std::endl;
}
};