From 65a3a11537512a715fa250004476a862c21af550 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Mon, 25 Apr 2022 10:42:31 +0200 Subject: [PATCH] Weitere Beispiele 21.4.2022 --- 20220421/hash-1.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 20220421/sets-24.cpp | 2 +- 20220421/sets-25.cpp | 27 +++++++++++++++++++++++++++ 20220421/sets-26.cpp | 21 +++++++++++++++++++++ 20220421/sets-27.cpp | 21 +++++++++++++++++++++ 20220421/sets-28.cpp | 27 +++++++++++++++++++++++++++ 20220421/sets-29.cpp | 29 +++++++++++++++++++++++++++++ 20220421/sets-30.cpp | 27 +++++++++++++++++++++++++++ 20220421/sets-31.cpp | 27 +++++++++++++++++++++++++++ 20220421/sets-32.cpp | 28 ++++++++++++++++++++++++++++ 20220421/sets-33.cpp | 28 ++++++++++++++++++++++++++++ 20220421/sets-34.pas | 27 +++++++++++++++++++++++++++ 20220421/sets-35.pas | 24 ++++++++++++++++++++++++ 20220421/sets-36.pas | 32 ++++++++++++++++++++++++++++++++ 20220421/sets-37.cpp | 26 ++++++++++++++++++++++++++ 20220421/sets-38.cpp | 26 ++++++++++++++++++++++++++ 16 files changed, 412 insertions(+), 1 deletion(-) create mode 100644 20220421/hash-1.cpp create mode 100644 20220421/sets-25.cpp create mode 100644 20220421/sets-26.cpp create mode 100644 20220421/sets-27.cpp create mode 100644 20220421/sets-28.cpp create mode 100644 20220421/sets-29.cpp create mode 100644 20220421/sets-30.cpp create mode 100644 20220421/sets-31.cpp create mode 100644 20220421/sets-32.cpp create mode 100644 20220421/sets-33.cpp create mode 100644 20220421/sets-34.pas create mode 100644 20220421/sets-35.pas create mode 100644 20220421/sets-36.pas create mode 100644 20220421/sets-37.cpp create mode 100644 20220421/sets-38.cpp diff --git a/20220421/hash-1.cpp b/20220421/hash-1.cpp new file mode 100644 index 0000000..25ebdb1 --- /dev/null +++ b/20220421/hash-1.cpp @@ -0,0 +1,41 @@ +// C++ program to demonstrate various function of unordered_set +#include <bits/stdc++.h> +using namespace std; + +int main() +{ + // declaring set for storing string data-type + unordered_set <string> stringSet ; + + // inserting various string, same string will be stored + // once in set + + stringSet.insert("code") ; + stringSet.insert("in") ; + stringSet.insert("c++") ; + stringSet.insert("is") ; + stringSet.insert("fast") ; + + string key = "slow" ; + + // find returns end iterator if key is not found, + // else it returns iterator to that key + + if (stringSet.find(key) == stringSet.end()) + cout << key << " not found" << endl << endl ; + else + cout << "Found " << key << endl << endl ; + + key = "c++"; + if (stringSet.find(key) == stringSet.end()) + cout << key << " not found\n" ; + else + cout << "Found " << key << endl ; + + // now iterating over whole set and printing its + // content + cout << "\nAll elements : "; + unordered_set<string> :: iterator itr; + for (itr = stringSet.begin(); itr != stringSet.end(); itr++) + cout << (*itr) << endl; +} diff --git a/20220421/sets-24.cpp b/20220421/sets-24.cpp index b19ea74..27cd796 100644 --- a/20220421/sets-24.cpp +++ b/20220421/sets-24.cpp @@ -13,11 +13,11 @@ bool less (int a, int b) int main () { - fwd = 0; std::set <int, decltype (less)*> prime ({ 2, 3, 7, 11, 13 }, less); for (auto p : prime) std::cout << p << " "; std::cout << std::endl; + fwd = 0; prime.insert (5); for (auto p : prime) std::cout << p << " "; diff --git a/20220421/sets-25.cpp b/20220421/sets-25.cpp new file mode 100644 index 0000000..69bf900 --- /dev/null +++ b/20220421/sets-25.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, fwd, std::placeholders::_1, std::placeholders::_2); + std::set <int, decltype (less2)*> prime ({ 2, 3, 7, 11, 13 }, &less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-26.cpp b/20220421/sets-26.cpp new file mode 100644 index 0000000..2147e4b --- /dev/null +++ b/20220421/sets-26.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, fwd, std::placeholders::_1, std::placeholders::_2); + std::cout << less2 (2, 4) << std::endl; + fwd = 0; + std::cout << less2 (2, 4) << std::endl; + return 0; +} diff --git a/20220421/sets-27.cpp b/20220421/sets-27.cpp new file mode 100644 index 0000000..bab0507 --- /dev/null +++ b/20220421/sets-27.cpp @@ -0,0 +1,21 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int *fwd, int a, int b) +{ + if (*fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, &fwd, std::placeholders::_1, std::placeholders::_2); + std::cout << less2 (2, 4) << std::endl; + fwd = 0; + std::cout << less2 (2, 4) << std::endl; + return 0; +} diff --git a/20220421/sets-28.cpp b/20220421/sets-28.cpp new file mode 100644 index 0000000..bb78147 --- /dev/null +++ b/20220421/sets-28.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, fwd, std::placeholders::_1, std::placeholders::_2); + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-29.cpp b/20220421/sets-29.cpp new file mode 100644 index 0000000..d0bcefa --- /dev/null +++ b/20220421/sets-29.cpp @@ -0,0 +1,29 @@ +#include <iostream> +#include <set> +#include <functional> +#include <typeinfo> + +bool less (int fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, fwd, std::placeholders::_1, std::placeholders::_2); + std::cout << typeid (less2).name () << std::endl; + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-30.cpp b/20220421/sets-30.cpp new file mode 100644 index 0000000..87bcbca --- /dev/null +++ b/20220421/sets-30.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int *fwd, int a, int b) +{ + if (*fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, &fwd, std::placeholders::_1, std::placeholders::_2); + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-31.cpp b/20220421/sets-31.cpp new file mode 100644 index 0000000..e81053c --- /dev/null +++ b/20220421/sets-31.cpp @@ -0,0 +1,27 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int &fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, fwd, std::placeholders::_1, std::placeholders::_2); + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-32.cpp b/20220421/sets-32.cpp new file mode 100644 index 0000000..f3f70cc --- /dev/null +++ b/20220421/sets-32.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int &fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, std::reference_wrapper <int> (fwd), + std::placeholders::_1, std::placeholders::_2); + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-33.cpp b/20220421/sets-33.cpp new file mode 100644 index 0000000..342b96a --- /dev/null +++ b/20220421/sets-33.cpp @@ -0,0 +1,28 @@ +#include <iostream> +#include <set> +#include <functional> + +bool less (int &fwd, int a, int b) +{ + if (fwd) + return a < b; + else + return a > b; +} + +int main () +{ + int fwd = 1; + auto less2 = std::bind (less, std::ref (fwd), + std::placeholders::_1, std::placeholders::_2); + std::set <int, std::function <bool (int, int)>> prime ({ 2, 3, 7, 11, 13 }, less2); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-34.pas b/20220421/sets-34.pas new file mode 100644 index 0000000..23506b5 --- /dev/null +++ b/20220421/sets-34.pas @@ -0,0 +1,27 @@ +program Sets; + +procedure Sort (a, b: Integer; function less (a, b: Integer): Boolean); +begin + if less (a, b) then + WriteLn (a, ' < ', b) + else + WriteLn (a, ' > ', b) +end; + +procedure DoSomething; + + function less (a, b: Integer): Boolean; + begin + if a < b then + less := true + else + less := false + end; + +begin + Sort (2, 4, less) +end; + +begin + DoSomething +end. diff --git a/20220421/sets-35.pas b/20220421/sets-35.pas new file mode 100644 index 0000000..015a047 --- /dev/null +++ b/20220421/sets-35.pas @@ -0,0 +1,24 @@ +program Sets; + +procedure Sort (a, b: Integer; function less (a, b: Integer): Boolean); +begin + if less (a, b) then + WriteLn (a, ' < ', b) + else + WriteLn (a, ' > ', b) +end; + +procedure DoSomething; + + function less (a, b: Integer): Boolean; + begin + less := a < b + end; + +begin + Sort (2, 4, less) +end; + +begin + DoSomething +end. diff --git a/20220421/sets-36.pas b/20220421/sets-36.pas new file mode 100644 index 0000000..6524fc8 --- /dev/null +++ b/20220421/sets-36.pas @@ -0,0 +1,32 @@ +program Sets; + +procedure Sort (a, b: Integer; function less (a, b: Integer): Boolean); +begin + if less (a, b) then + WriteLn (a, ' < ', b) + else + WriteLn (a, ' > ', b) +end; + +procedure DoSomething; + +var + fwd: Boolean = true; + + function less (a, b: Integer): Boolean; + begin + if fwd then + less := a < b + else + less := a > b + end; + +begin + Sort (2, 4, less); + fwd := false; + Sort (2, 4, less) +end; + +begin + DoSomething +end. diff --git a/20220421/sets-37.cpp b/20220421/sets-37.cpp new file mode 100644 index 0000000..645c0dc --- /dev/null +++ b/20220421/sets-37.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <set> + +int main () +{ + bool fwd = 1; + + auto less = [&] (int a, int b) + { + if (fwd) + return a < b; + else + return a > b; + }; + + std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} diff --git a/20220421/sets-38.cpp b/20220421/sets-38.cpp new file mode 100644 index 0000000..5945f93 --- /dev/null +++ b/20220421/sets-38.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <set> + +int main () +{ + bool fwd = 1; + + auto less = [=] (int a, int b) + { + if (fwd) + return a < b; + else + return a > b; + }; + + std::set <int, decltype (less)> prime ({ 2, 3, 7, 11, 13 }, less); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + fwd = 0; + prime.insert (5); + for (auto p : prime) + std::cout << p << " "; + std::cout << std::endl; + return 0; +} -- GitLab