Skip to content
Snippets Groups Projects
Commit 65a3a115 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Weitere Beispiele 21.4.2022

parent 0772ad4d
Branches
No related tags found
No related merge requests found
// 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;
}
......@@ -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 << " ";
......
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
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.
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.
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.
#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;
}
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment