x031.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <iomanip>
5 #include <iostream>
6 #include <iterator>
7 #include <numeric>
8 #include <vector>
9 
10 namespace nmx::apps::x031 {
11 
15 inline void ex0() {
16  //erzeuge leeren Vektor
17  std::vector<double> v;
18 
19  // fülle Vektor
20  for (double x = 0; x < 1; x += 0.23) {
21  v.push_back(x);
22  }
23 
24  //gebe Elemente mittels Iteratoren
25  //des Vektors auf dem Bildschirm aus
26  for (auto itr = v.begin(); itr != v.end(); itr++) {
27  std::cout << *itr << ",";
28  }
29  std::cout << std::endl;
30 
31  // gebe Elemente mit range-based for-loop
32  //auf dem Bildschirm aus
33  for (const auto &x : v) {
34  std::cout << x << ",";
35  }
36 
37  std::cout << std::endl;
38 }
39 
43 inline void ex1() {
44  //Speicher wird reserviert
45  std::vector<double> v1(9);
46 
47  //fülle Vektor mit Werten von -4,...4
48  std::iota(v1.begin(), v1.end(), -4);
49  //gebe die Werte aus
50  for (const auto &x : v1) {
51  std::cout << x << ",";
52  }
53  std::cout << std::endl;
54 
55  //kopiere Vektor
56  auto v2 = v1;
57 
58  //drehe die Reihenfolge um
59  std::reverse(v2.begin(), v2.end());
60 
61  //gebe Elemente von v1 und v2 auf dem Bildschirm aus
62  auto outitr = std::ostream_iterator<double>(std::cout, ",");
63  std::copy(v1.begin(), v1.end(), outitr);
64  std::cout << std::endl;
65  std::copy(v2.begin(), v2.end(), outitr);
66  std::cout << std::endl;
67 
68  //addiere alle Elemente des Vektors mit Startwert 0
69  auto val = std::accumulate(v1.begin(), v1.end(), 0);
70  std::cout << val << std::endl;
71 
72  //Skalarprodukt v1 * v2
73  auto val1 = std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);
74  std::cout << val1 << std::endl;
75 }
76 
77 } // namespace nmx::apps::x031
void ex1()
ex1 Anwendung von Algorithmen auf einen Container
Definition: x031.h:43
void ex0()
ex0 Speicherung von Daten in einem Container
Definition: x031.h:15