diff --git a/20220421/hash-1.cpp b/20220421/hash-1.cpp new file mode 100644 index 0000000000000000000000000000000000000000..25ebdb1c261382e58f3b0a1f6727e77246fb14ee --- /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 b19ea745e479fd38be4d3c56f95dfbd1c5be36c6..27cd796763a8d6056c03bf175fa446c2deb8d416 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 0000000000000000000000000000000000000000..69bf90020efdcadd104484c4118fab471332c227 --- /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 0000000000000000000000000000000000000000..2147e4b47b6809ce3361418d50a3770b63f1520f --- /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 0000000000000000000000000000000000000000..bab050767ad8131d662fef0636e4c13d12143529 --- /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 0000000000000000000000000000000000000000..bb78147b94fd19826d85215b64632611ad01c564 --- /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 0000000000000000000000000000000000000000..d0bcefa7f6687dfdc4acf610d9e4d591770f45f4 --- /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 0000000000000000000000000000000000000000..87bcbcadf2e3d3ef1c1a5df1e1a7a0b633861f65 --- /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 0000000000000000000000000000000000000000..e81053c78b8a3687a764550b3f379438e0cf7b9c --- /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 0000000000000000000000000000000000000000..f3f70cc82d187bf1cede45c17e11cb242c26dd80 --- /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 0000000000000000000000000000000000000000..342b96a9d5704c2a97838aa7d6add685f544f36a --- /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 0000000000000000000000000000000000000000..23506b5fccc2e57689904db259802a9d59100b11 --- /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 0000000000000000000000000000000000000000..015a047d7f3f526aeb634c05f3693e69132ff88c --- /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 0000000000000000000000000000000000000000..6524fc810be0b787d37d8db535896af48c16eb1f --- /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 0000000000000000000000000000000000000000..645c0dc954ff57c59c9267082216393fd4418d48 --- /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 0000000000000000000000000000000000000000..5945f9399059de2fee5f3fab3df4be56a8fc5749 --- /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; +}