37 lines
909 B
C++
37 lines
909 B
C++
/// @file
|
|
/// @brief task C: transform class Vector from task A to a class template
|
|
|
|
#pragma once
|
|
#include <vector>
|
|
#include <functional>
|
|
#include <iostream>
|
|
|
|
/// @todo copy Vector from taskA.Vector.hpp and adapt according to task C
|
|
template <typename T>
|
|
class Vector {
|
|
std::vector<T> data_;
|
|
|
|
public:
|
|
Vector(std::vector<T> data) : data_(data) {}
|
|
Vector(T start, T end, unsigned int N) {
|
|
T 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<T()> 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;
|
|
}
|
|
}; |