From 6f504f3bf1871a82a2409d4155dc5127dc9c0e61 Mon Sep 17 00:00:00 2001
From: Peter Gerwinski <peter.gerwinski@hs-bochum.de>
Date: Tue, 11 Apr 2023 07:59:51 +0200
Subject: [PATCH] Weitere Beispiele 6.4.2023: Iteratoren, Templates

---
 20230406/aux/hallo.txt    |  1 +
 20230406/iterators-01.cpp |  9 ++++++
 20230406/iterators-02.cpp |  9 ++++++
 20230406/iterators-03.cpp | 10 ++++++
 20230406/iterators-04.cpp | 10 ++++++
 20230406/iterators-05.cpp | 14 +++++++++
 20230406/iterators-06.cpp | 14 +++++++++
 20230406/iterators-07.cpp | 14 +++++++++
 20230406/iterators-08.cpp | 14 +++++++++
 20230406/iterators-09.cpp | 14 +++++++++
 20230406/templates-01.cpp | 23 ++++++++++++++
 20230406/templates-02.cpp | 19 ++++++++++++
 20230406/templates-03.cpp | 17 +++++++++++
 20230406/templates-04.cpp | 24 +++++++++++++++
 20230406/templates-07.cpp | 35 +++++++++++++++++++++
 20230406/templates-08.cpp | 41 +++++++++++++++++++++++++
 20230406/templates-09.cpp | 41 +++++++++++++++++++++++++
 20230406/templates-10.cpp | 54 +++++++++++++++++++++++++++++++++
 20230406/templates-11.cpp | 56 ++++++++++++++++++++++++++++++++++
 20230406/templates-12.cpp | 56 ++++++++++++++++++++++++++++++++++
 20230406/templates-13.cpp | 61 +++++++++++++++++++++++++++++++++++++
 20230406/templates-14.cpp | 54 +++++++++++++++++++++++++++++++++
 20230406/templates-15.cpp | 54 +++++++++++++++++++++++++++++++++
 20230406/templates-16.cpp | 64 +++++++++++++++++++++++++++++++++++++++
 24 files changed, 708 insertions(+)
 create mode 100644 20230406/aux/hallo.txt
 create mode 100644 20230406/iterators-01.cpp
 create mode 100644 20230406/iterators-02.cpp
 create mode 100644 20230406/iterators-03.cpp
 create mode 100644 20230406/iterators-04.cpp
 create mode 100644 20230406/iterators-05.cpp
 create mode 100644 20230406/iterators-06.cpp
 create mode 100644 20230406/iterators-07.cpp
 create mode 100644 20230406/iterators-08.cpp
 create mode 100644 20230406/iterators-09.cpp
 create mode 100644 20230406/templates-01.cpp
 create mode 100644 20230406/templates-02.cpp
 create mode 100644 20230406/templates-03.cpp
 create mode 100644 20230406/templates-04.cpp
 create mode 100644 20230406/templates-07.cpp
 create mode 100644 20230406/templates-08.cpp
 create mode 100644 20230406/templates-09.cpp
 create mode 100644 20230406/templates-10.cpp
 create mode 100644 20230406/templates-11.cpp
 create mode 100644 20230406/templates-12.cpp
 create mode 100644 20230406/templates-13.cpp
 create mode 100644 20230406/templates-14.cpp
 create mode 100644 20230406/templates-15.cpp
 create mode 100644 20230406/templates-16.cpp

diff --git a/20230406/aux/hallo.txt b/20230406/aux/hallo.txt
new file mode 100644
index 0000000..8095a18
--- /dev/null
+++ b/20230406/aux/hallo.txt
@@ -0,0 +1 @@
+Hallo, Welt!
diff --git a/20230406/iterators-01.cpp b/20230406/iterators-01.cpp
new file mode 100644
index 0000000..3429606
--- /dev/null
+++ b/20230406/iterators-01.cpp
@@ -0,0 +1,9 @@
+#include <iostream>
+
+int main ()
+{
+  int prime[5] = { 2, 3, 5, 7, 11 };
+  for (int *p = prime; p != prime + 5; p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-02.cpp b/20230406/iterators-02.cpp
new file mode 100644
index 0000000..13b3f45
--- /dev/null
+++ b/20230406/iterators-02.cpp
@@ -0,0 +1,9 @@
+#include <iostream>
+
+int main ()
+{
+  int prime[5] = { 2, 3, 5, 7, 11 };
+  for (int *p = prime; p <= prime + 4; p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-03.cpp b/20230406/iterators-03.cpp
new file mode 100644
index 0000000..7143024
--- /dev/null
+++ b/20230406/iterators-03.cpp
@@ -0,0 +1,10 @@
+#include <iostream>
+#include <array>
+
+int main ()
+{
+  std::array <int, 5> prime = { { 2, 3, 5, 7, 11 } };
+  for (std::array <int, 5>::iterator p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-04.cpp b/20230406/iterators-04.cpp
new file mode 100644
index 0000000..3b04a80
--- /dev/null
+++ b/20230406/iterators-04.cpp
@@ -0,0 +1,10 @@
+#include <iostream>
+#include <vector>
+
+int main ()
+{
+  std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-05.cpp b/20230406/iterators-05.cpp
new file mode 100644
index 0000000..7c62ad8
--- /dev/null
+++ b/20230406/iterators-05.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <vector>
+
+int main ()
+{
+  std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  prime.push_back (13);
+  prime.push_back (17);
+  for (std::vector <int>::iterator p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-06.cpp b/20230406/iterators-06.cpp
new file mode 100644
index 0000000..8698385
--- /dev/null
+++ b/20230406/iterators-06.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <vector>
+
+int main ()
+{
+  std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (auto p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  prime.push_back (13);
+  prime.push_back (17);
+  for (auto p = prime.begin (); p != prime.end (); p++)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-07.cpp b/20230406/iterators-07.cpp
new file mode 100644
index 0000000..e7ca0b0
--- /dev/null
+++ b/20230406/iterators-07.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <vector>
+
+int main ()
+{
+  std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (auto p : prime)
+    std::cout << *p << std::endl;
+  prime.push_back (13);
+  prime.push_back (17);
+  for (auto p : prime)
+    std::cout << *p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-08.cpp b/20230406/iterators-08.cpp
new file mode 100644
index 0000000..80328d1
--- /dev/null
+++ b/20230406/iterators-08.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <vector>
+
+int main ()
+{
+  std::vector <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (auto &p : prime)
+    std::cout << p << std::endl;
+  prime.push_back (13);
+  prime.push_back (17);
+  for (auto &p : prime)
+    std::cout << p << std::endl;
+  return 0;
+}
diff --git a/20230406/iterators-09.cpp b/20230406/iterators-09.cpp
new file mode 100644
index 0000000..a1c2005
--- /dev/null
+++ b/20230406/iterators-09.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include <list>
+
+int main ()
+{
+  std::list <int> prime = { { 2, 3, 5, 7, 11 } };
+  for (auto &p : prime)
+    std::cout << p << std::endl;
+  prime.push_back (13);
+  prime.push_back (17);
+  for (auto &p : prime)
+    std::cout << p << std::endl;
+  return 0;
+}
diff --git a/20230406/templates-01.cpp b/20230406/templates-01.cpp
new file mode 100644
index 0000000..b6d013e
--- /dev/null
+++ b/20230406/templates-01.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <string>
+
+using std::cout;
+using std::endl;
+using std::string;
+
+void print (int x)
+{
+  cout << x << endl;
+}
+
+void print (string x)
+{
+  cout << x << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  return 0;
+}
diff --git a/20230406/templates-02.cpp b/20230406/templates-02.cpp
new file mode 100644
index 0000000..191e300
--- /dev/null
+++ b/20230406/templates-02.cpp
@@ -0,0 +1,19 @@
+#include <iostream>
+#include <string>
+
+using std::cout;
+using std::endl;
+using std::string;
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  return 0;
+}
diff --git a/20230406/templates-03.cpp b/20230406/templates-03.cpp
new file mode 100644
index 0000000..6166f19
--- /dev/null
+++ b/20230406/templates-03.cpp
@@ -0,0 +1,17 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  return 0;
+}
diff --git a/20230406/templates-04.cpp b/20230406/templates-04.cpp
new file mode 100644
index 0000000..4fca671
--- /dev/null
+++ b/20230406/templates-04.cpp
@@ -0,0 +1,24 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+class bla
+{
+  int content;
+};
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  bla blubb;
+  print (blubb);
+  return 0;
+}
diff --git a/20230406/templates-07.cpp b/20230406/templates-07.cpp
new file mode 100644
index 0000000..4279b33
--- /dev/null
+++ b/20230406/templates-07.cpp
@@ -0,0 +1,35 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+class bla
+{
+public:
+  int content;
+  bla (int x):
+    content (x)
+  {
+  }
+};
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+template <>
+void print (bla x)
+{
+  cout << x.content << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  bla blubb (137);
+  print (blubb);
+  return 0;
+}
diff --git a/20230406/templates-08.cpp b/20230406/templates-08.cpp
new file mode 100644
index 0000000..a0c3a29
--- /dev/null
+++ b/20230406/templates-08.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+class bla
+{
+public:
+  int content;
+  bla (int x):
+    content (x)
+  {
+  }
+};
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+template <>
+void print (int x)
+{
+  cout << "Die Antwort lautet: " << x << endl;
+}
+
+template <>
+void print (bla x)
+{
+  cout << x.content << endl;
+}
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  bla blubb (137);
+  print (blubb);
+  return 0;
+}
diff --git a/20230406/templates-09.cpp b/20230406/templates-09.cpp
new file mode 100644
index 0000000..2606961
--- /dev/null
+++ b/20230406/templates-09.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+
+using std::cout;
+using std::endl;
+
+class bla
+{
+public:
+  int content;
+  bla (int x):
+    content (x)
+  {
+  }
+};
+
+template <typename t>
+void print (t x)
+{
+  cout << x << endl;
+}
+
+template <>
+void print (int x)
+{
+  cout << "Die Antwort lautet: " << x << endl;
+}
+
+template <>
+void print (bla x)
+{
+  cout << x << endl;  // Fehler wird erkannt, obwohl nicht instantiiert.
+}                     // Dies ist nicht immer der Fall.
+
+int main ()
+{
+  print (42);
+  print ("Hello, world!");
+  bla blubb (137);
+//  print (blubb);
+  return 0;
+}
diff --git a/20230406/templates-10.cpp b/20230406/templates-10.cpp
new file mode 100644
index 0000000..88fc5a1
--- /dev/null
+++ b/20230406/templates-10.cpp
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (list *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  return 0;
+}
diff --git a/20230406/templates-11.cpp b/20230406/templates-11.cpp
new file mode 100644
index 0000000..57aec67
--- /dev/null
+++ b/20230406/templates-11.cpp
@@ -0,0 +1,56 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (list *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  s.output ();
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  i.output ();
+  return 0;
+}
diff --git a/20230406/templates-12.cpp b/20230406/templates-12.cpp
new file mode 100644
index 0000000..e5d6547
--- /dev/null
+++ b/20230406/templates-12.cpp
@@ -0,0 +1,56 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (node *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  s.output ();
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  i.output ();
+  return 0;
+}
diff --git a/20230406/templates-13.cpp b/20230406/templates-13.cpp
new file mode 100644
index 0000000..3577e9f
--- /dev/null
+++ b/20230406/templates-13.cpp
@@ -0,0 +1,61 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (node *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+void blabla (int x)
+{
+  huddeldibuddel (wuppdich);
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  s.output ();
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  i.output ();
+  return 0;
+}
diff --git a/20230406/templates-14.cpp b/20230406/templates-14.cpp
new file mode 100644
index 0000000..b42e532
--- /dev/null
+++ b/20230406/templates-14.cpp
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (huddeldibuddel *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  return 0;
+}
diff --git a/20230406/templates-15.cpp b/20230406/templates-15.cpp
new file mode 100644
index 0000000..e8296e0
--- /dev/null
+++ b/20230406/templates-15.cpp
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (char *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  return 0;
+}
diff --git a/20230406/templates-16.cpp b/20230406/templates-16.cpp
new file mode 100644
index 0000000..d023eee
--- /dev/null
+++ b/20230406/templates-16.cpp
@@ -0,0 +1,64 @@
+#include <iostream>
+#include <string>
+
+using namespace std;
+
+template <typename argtype>
+class list
+{
+  struct node
+  {
+    argtype content;
+    node *next;
+  };
+  node *root;
+public:
+  list ();
+  void insert (argtype x);
+  void output ();
+};
+
+template <typename argtype>
+list <argtype>::list ()
+{
+  root = NULL;
+}
+
+template <typename argtype>
+void list <argtype>::insert (argtype x)
+{
+  node *n = new node;
+  n->content = x;
+  n->next = root;
+  root = n;
+}
+
+template <typename argtype>
+void list <argtype>::output ()
+{
+  for (node *p = root; p; p = p->next)
+    cout << p->content << endl;
+}
+
+template <>
+void list <int>::output ()
+{
+  for (node *p = root; p; p = p->next)
+    cout << p->content << " ";
+  cout << endl;
+}
+
+int main ()
+{
+  list <string> s;
+  s.insert ("one");
+  s.insert ("two");
+  s.insert ("three");
+  s.output ();
+  list <int> i;
+  i.insert (1);
+  i.insert (2);
+  i.insert (3);
+  i.output ();
+  return 0;
+}
-- 
GitLab