STL
main.cpp
Go to the documentation of this file.
1 // Einfuehrung in die STL
2 // Container, Algorithmen, Iteratoren
3 //
4 // Demo mit vector<double>
5 // copy
6 // sort
7 // find
8 // remove, erase
9 
10 // sort mit Praedikatorfunktion, Funktionsobjekt
11 // count
12 // count_if
13 //
14 #include "komplex.h"
15 #include <algorithm>
16 #include <cmath>
17 #include <iostream>
18 #include <iterator>
19 #include <list>
20 #include <numeric>
21 #include <vector>
22 using namespace std;
23 
24 // Ausgabe von vector
25 template <class T>
26 ostream& operator<<(ostream& s, const vector<T>& x)
27 {
28  for (unsigned int i=0; i<x.size(); ++i)
29  {
30  s << x[i] << " ";
31  }
32  return s;
33 }
34 
35 
36 template <class T>
37 ostream& operator<<(ostream& s, const list<T>& x)
38 {
39 // for (const auto &pi: x)
40 // {
41 // s << pi << " ";
42 // }
43  copy(x.cbegin(), x.cend(), ostream_iterator<T>(s, " "));
44  return s;
45 }
46 
47 // binaere Praedikatorfunktionen
48 bool IsLarger (double i,double j);
49 
50 bool IsLarger (double i,double j)
51 {
52  return (i>j);
53 }
54 
55 bool IsLargerAbs (double i,double j);
56 
57 bool IsLargerAbs (double i,double j)
58 {
59  return std::abs(i)>std::abs(j);
60 }
61 
62 // unaere Praedikatorfunktionen
63 bool IsNegative(double i);
64 
65 bool IsNegative(double i)
66 {
67  return i<0;
68 }
69 
70 int main()
71 {
72  cout << "Hello world!" << endl;
73 
74 // Demo STL mit vector<double>
75  vector<double> v{-1,-3,-3,4,-1,4};
76  cout << v << endl;
77 
78  // kopiere
79  vector<double> z;
80  // vorher den Speicher fuer den Zielvektor anfordern !!
81 // Variante 1 (exakte Preallokierung von z)
82  z.resize(v.size());
83  copy(v.begin(),v.end(),z.begin());
84 // Variante 2 (ueberschaetzte Preallokierung von z)
85 // z.resize(2*v.size());
86 // auto it_1 = copy(v.begin(),v.end(),z.begin());
87 // z.erase(it_1,z.end());
88  cout << z << endl;
89 
90  // Finden
91 // vector<double>::iterator it;
92 // it = find(v.begin(),v.end(),4);
93  auto it = find(v.begin(),v.end(),4);
94  if (it != v.end()) // wirklich etwas gefunden ?
95  {
96  cout << "gefunden " << *it << endl;
97  }
98  cout << it - v.begin() << endl; // Index (nur bei vector)
99 
100  sort(v.begin(),v.end());
101  cout << " sort (aufsteigend) " << v << endl;
102  // Entferne alle mehrfachen Elemente hintereinander
103  it = unique(v.begin(),v.end()); // nach vorn schieben
104  cout << v << endl;
105  cout << it - v.begin() << endl; // Index (nur bei vector)
106 // Wirkliches Entfernen aus dem Vektor
107  v.erase(it,v.end());
108  cout << v << endl;
109 
110 // absteigend sortieren
111  sort(v.begin(),v.end(),greater<double>()); // Erfordert double::operator>
112  sort(v.begin(),v.end(),IsLarger); // binaere Praedikatorfunktion
113  sort(v.begin(),v.end(), [](auto a, auto b){return a>b;} ); // dasselbe via Lambda-Fkt
114  cout << v << endl;
115 
116 // Finde das erste negative element
117  auto ip = find_if(v.begin(),v.end(),IsNegative);
118  cout << "erste negative Zahl: " << *ip << endl;
119 
120 // Desgleichen via Lambda-Funktion [C++11]
121  auto ip2 = find_if(v.begin(),v.end(),
122  [](double a) -> bool { return a<0.0; }
123  );
124  cout << "erste negative Zahl: " << *ip2 << endl;
125 
126  // Zaehlen von Elemente
127  int p = count(z.begin(),z.end(),-3);
128  cout << " z : " << z << endl;
129  cout << "p(-3) :" << p << endl;
130  p = count_if(z.begin(),z.end(),IsNegative);
131  cout << "p(neg.) :" << p << endl;
132 
133  cout << "#############################################\n";
134 
135 // Demo STL mit vector<Komplex>
136  vector<Komplex> kv{Komplex(1,3), Komplex(2,5), Komplex(-1,3), Komplex(1,4),Komplex(1,3), Komplex(1,3) };
137  const vector<Komplex> kz(kv);
138  cout << kv << endl;
139 
140  sort(kv.begin(),kv.end()); // braucht: Komplex::operator< und Komplex::operator=
141  cout << kv << endl;
142 
143  auto ik=find(kv.begin(),kv.end(), Komplex(1,4)); // braucht: Komplex::operator==
144  if (ik != kv.end()) // wirklich etwas gefunden ?
145  {
146  cout << "gefunden " << *ik << endl;
147  }
148 
149  // Anzahl der Elemente mit |re| < 2;
150  double const vergl=2.0;
151  int n2 = count_if(cbegin(kv),cend(kv),
152  [vergl] (auto const &a) {return std::abs(a.Get_re())<vergl;}
153  );
154  cout << "|re| < 2 in " << n2 << " Elementen." << endl;
155 
156 
157  cout << "-------- jetzt Liste -----------\n";
158  list<Komplex> al(kz.begin(),kz.end()); // Allokiere und kopiere von unsortiertem Vector
159  //list<Komplex> al; // leere Liste
160  //al.assign(kz.begin(),kz.end()); // Allokiere und kopiere von unsortiertem Vector
161  cout << al << endl;
162  al.sort(); // braucht: Komplex::operator< und Komplex::operator=
163  cout << al << endl;
164 
165  // Finde einen Eintrag mit Imaginärteil==3, via Lambda-Funktion
166  double fim{3.0};
167  list<Komplex> ip_al = find(cbegin(al),cend(al),
168  [const fim](Komplex const & a)
169  { return fim==a.Get_im(); }
170  );
171 
172 
173  return 0;
174 }
Definition: komplex.h:7
bool IsLargerAbs(double i, double j)
Definition: main.cpp:57
bool IsNegative(double i)
Definition: main.cpp:65
bool IsLarger(double i, double j)
Definition: main.cpp:50
ostream & operator<<(ostream &s, const vector< T > &x)
Definition: main.cpp:26
int main()
Definition: main.cpp:70