pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Sioggis Mist.
Posted by Anonymous on Tue 19th Nov 2024 16:09
raw | new post

  1. // Type your code here, or load an example.
  2. #include <format>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <iostream>
  6. #include <functional>
  7. // #include <filesystem>
  8.  
  9. using namespace std;
  10.  
  11. struct FileInfo {
  12.   string Name; // Name der Datei
  13.   long long Length; // Dateigröße
  14. };
  15.  
  16. struct MySmaler {
  17.     bool operator () (const std::string & a, const std::string & b){
  18.         return a < b;
  19.     };
  20. };
  21.  
  22.  
  23. struct MyGrater {
  24.     bool operator () (const FileInfo & a, const FileInfo & b){
  25.         return a.Length < b.Length;
  26.     };
  27. };
  28.  
  29. bool greaterFile( FileInfo * pA, FileInfo * pB ) {
  30.     return pA->Length < pB->Length;
  31. }
  32.  
  33. void test()
  34. {
  35.     std::vector<std::string> vi{ "Du", "Wir", "Lila", "Alle", "Andren"};
  36.     std::sort(vi.begin(), vi.end(), std::greater<std::string>());
  37.  
  38.     int tütÖÄ = 42;
  39.  
  40.     for( auto x : vi ) {
  41.         std::cout << x << " ";
  42.     }
  43.     std::cout << std::endl;
  44.     std::sort(vi.begin(), vi.end(), MySmaler());
  45.     for( auto x : vi ) {
  46.         std::cout << x << " ";
  47.     }
  48.     std::cout << "\nFiles (" << tütÖÄ << "):\n";
  49.  
  50.     std::vector<FileInfo> vf {{"aa", 1234}, {"bbb", 4711}, {"LauneBär", 123456789012}};
  51.     std::sort(vf.begin(), vf.end(), MyGrater());
  52.     for( auto f : vf ) {
  53.         std::cout << f.Name  << ": " << f.Length << endl;
  54.     }
  55.     std::cout << std::endl;
  56.  
  57.    
  58.     std::vector<FileInfo *> vpf;
  59.     std::sort(vpf.begin(), vpf.end(), greaterFile);
  60.  
  61.     [](int i) {cout << i << endl; }(5);
  62.  
  63.     [tütÖÄ](const char * name) { cout << "lambda - " << name << " : " << tütÖÄ << endl; } ( "jaja");
  64.  
  65.  
  66.     std::function<void()> f1 = []() {cout << "a" << endl; } ;
  67.     std::function<void(void)> f2 = []() {cout << "b" << endl; } ; ;
  68.     std::function<int(int x, std::string y)> f3 = [](int x, std::string s ) {cout << x << " - " << s << endl; return x; };  
  69.     std::function<std::string(int x)> f4 = [](int x ) {cout << x << " - " << endl; return string("Hallo"); };
  70.     std::function<std::string(int x, std::function<std::string(int)>)> f5 =  [](int x, std::function<std::string(int)> _fx ) {cout << x << " - " << _fx(x) << endl; return  _fx(x); };
  71.  
  72.     f1();
  73.     f2();
  74.     cout << f3(23, "Testen macht Spass!!") << endl;
  75.     cout << f5(42, f4) << endl;
  76.  
  77. }
  78.  
  79. int main() {
  80.     test();
  81.     return 0;
  82. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at