Für Vorlesungen, bitte die Webseite verwenden. https://flavigny.de/lecture
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

141 satır
4.6KB

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. // a class implementing a mathematical vector
  6. template <class K, size_t N>
  7. class Vector {
  8. public:
  9. // constructor, initializes vector with zeroes of length N
  10. Vector() : _vector(N, 0) {}
  11. // initialize by passing vector vec to initialize the vector object with
  12. Vector(std::vector<K> vec) { _vector = vec; }
  13. // copy constructor, assignment, destructor not needed
  14. // all handled by std::vector
  15. // reference [] operator, used for assignments
  16. K& operator[](int i) {
  17. return _vector[i];
  18. }
  19. // const ref [] operator
  20. K operator[](int i) const {
  21. return _vector[i];
  22. }
  23. // return an iterator pointing to the first element of the underlying std::vector
  24. typename std::vector<K>::const_iterator begin() const {
  25. return _vector.begin();
  26. }
  27. // return an iterator pointing to the last element of the underlying std::vector
  28. typename std::vector<K>::const_iterator end() const {
  29. return _vector.end();
  30. }
  31. // dot product of two vectors of the same length
  32. template <class K2>
  33. K operator*(const Vector<K2,N>& y) {
  34. std::vector<K> result(N); // new empty std::vector of correct size
  35. // now multiply one element of the first with one of the second vector
  36. std::transform(_vector.begin(), _vector.end(), y.begin(), result.begin(), std::multiplies<K>());
  37. // and sum up the products
  38. return std::accumulate(result.begin(), result.end(), 0);
  39. }
  40. // vector addition
  41. template <class K2>
  42. Vector<K, N> operator+(const Vector<K2, N>& y) {
  43. std::vector<K> result(N); // new empty std::vector of correct size
  44. // add elements by adding one element of the first to one of the second vector
  45. std::transform(_vector.begin(), _vector.end(), y.begin(), result.begin(), std::plus<K>());
  46. return Vector(result);
  47. }
  48. // return the maximum value in the vector
  49. K max() {
  50. return *std::max_element(_vector.begin(), _vector.end());
  51. }
  52. // return the minimum value in the vector
  53. K min() {
  54. return *std::min_element(_vector.begin(), _vector.end());
  55. }
  56. // return the average value of the vector
  57. K average() {
  58. // sum up all elements
  59. K sum = std::accumulate(_vector.begin(), _vector.end(), 0);
  60. return sum / N; // and divide by length
  61. }
  62. // run the specified function on each vector element
  63. template <class F>
  64. Vector<K,N> map(F op) const {
  65. std::vector<K> result(N); // initialize new std::vector of correct size
  66. // run op on each element
  67. std::transform(_vector.begin(), _vector.end(), result.begin(), op);
  68. return Vector(result);
  69. }
  70. private:
  71. std::vector<K> _vector; // the private std::vector container
  72. };
  73. // print vector
  74. template <class K, size_t N>
  75. std::ostream& operator<<(std::ostream& os, const Vector<K,N>& v) {
  76. os << "["; // opening brackets
  77. for (size_t i = 0; i < N; i++) {
  78. os << v[i]; // print element
  79. if(i < N-1) {
  80. os << ", "; // if not the last one, add comma
  81. }
  82. }
  83. os << "]"; // closing brackets
  84. return os;
  85. }
  86. // scalar multiplication for vector * scalar
  87. template <class K, size_t N, class K2>
  88. Vector<K, N> operator*(const Vector<K,N>& v, const K2 k) {
  89. // multiply a scalar value using the generic map function
  90. return v.map(std::bind1st(std::multiplies<K>(), k));
  91. }
  92. // scalar multiplication for scalar * vector
  93. template <class K, size_t N, class K2>
  94. Vector<K, N> operator*(const K2 k, const Vector<K,N>& v) {
  95. return v * k;
  96. }
  97. // example function
  98. double foo(double val) {
  99. return val*2+3;
  100. }
  101. // testing out functionalities
  102. int main() {
  103. Vector<double, 3> a;
  104. Vector<float, 3> b;
  105. a[0] = 0;
  106. a[1] = 0;
  107. a[2] = 4;
  108. b[0] = 42;
  109. b[1] = -20;
  110. b[2] = 3;
  111. std::cout << "Vektor a: " << a << ", Vektor b: " << b << std::endl;
  112. std::cout << "a+b: " << a+b << std::endl;
  113. std::cout << "a*b: " << a*b << std::endl;
  114. std::cout << "42*b: " << 42*b << std::endl;
  115. std::cout << "b*42: " << b*42 << std::endl;
  116. std::cout << "max(a): " << a.max() << std::endl;
  117. std::cout << "min(a): " << a.min() << std::endl;
  118. std::cout << "mean(a): " << a.average() << std::endl;
  119. std::cout << "foo angewendet auf b: " << b.map(foo) << std::endl;
  120. }