@ü.li:Listing 1: Berechnung des Wahrheitswertes mit Variadic Templates und fold expression @li:1 #include 2 3 bool allVar(){ 4 return true; 5 } 6 7 template 8 bool allVar(T t, Ts ... ts){ 9 return t && allVar(ts...); 10 } 11 12 template 13 bool all(Args... args) { return (... && args); } 14 15 int main(){ 16 17 std::cout << std::boolalpha; 18 19 std::cout << "allVar(): " << allVar() << std::endl; 20 std::cout << "all(): " << all() << std::endl; 21 22 std::cout << "allVar(true): " << allVar(true) << std::endl; 23 std::cout << "all(true): " << all(true) << std::endl; 24 25 std::cout << "allVar(true, true, true, false): " << allVar(true, true, true, false) << std::endl; 26 std::cout << "all(true, true, true, false): " << all(true, true, true, false) << std::endl; } @ü.li:Listing 2: Bedarfsauswertung mit der Range-Bibliothek @li:1 #include 2 #include 3 #include 4 5 using namespace ranges; 6 7 int main(){ 8 9 std::cout << std::endl; 10 11 // take 5 [1..] -- [1,2,3,4,5] 12 13 auto num = view::take(view::ints(1),5); 14 ranges::for_each(num, [](int i){ 15 std::cout << i << " "; 16 }); 17 18 std::cout << "\n\n"; 19 20 auto pairs= view::zip_with([](int i, char c){ return std::make_pair(i,c);},view::ints(0),"ranges"); 21 22 ranges::for_each(pairs, [](std::pair p){ 23 std::cout << "(" << p.first << ":" << p.second << ")"; 24 }); 25 26 std::cout << "\n\n"; 27 28 } @ü.li:Listing 3: Funktionskompostion mit der Range-Bibliothek @li:1 #include 2 #include 3 #include 4 5 using namespace ranges; 6 7 int main(){ 8 9 std::cout << std::endl; 10 11 // odds= takeWhile (< 1000) . filter odd . map (^2) 12 // odds [1..] -- [1,9,25, ... , 841,961] 13 14 auto odds= view::transform([](int i){return i*i;}) | 15 view::remove_if([](int i){return i % 2 == 0; }) | 16 view::take_while([](int i){return i < 1000;}); 17 auto oddNumbers= view::ints(1) | odds; 18 19 ranges::for_each(oddNumbers, [](int i){ 20 std::cout << i << " "; 21 }); 22 23 std::cout << "\n\n"; 24 25 // total= sum $ take 100 $ map (\x -> x*x) [100..1000] -- 2318350 26 27 auto total= ranges::accumulate(view::ints(100,1000) | 28 view::transform([](int x){return x*x;}) | 29 view::take(100), 0); 30 31 std::cout << "total: " << total << std::endl; 32 33 std::cout << std::endl; 34 35 } @ü.li:Listing 4: List versus Range Comprehension @li:1 #include 2 #include 3 4 using namespace ranges; 5 6 int main(){ 7 8 std::cout << std::endl; 9 10 // odds= [ x*x | x <-[1..] , odd x ] 11 // takeWhile (<1000) odds -- -- [1,9,25, ... , 841,961] 12 13 auto odds= view::ints(1) | view::for_each([](int i){return yield_if(i%2 == 1,i*i);}); 14 15 ranges::for_each(odds | view::take_while([](int i){ return i < 1000;}) , [](int i){ 16 std::cout << i << " "; 17 }); 18 19 std::cout << "\n\n"; 20 21 } @ü.li:Listing 5: Pythagoreisches Tripel mit List und Range Comprehension @li:1 triples =[(x, y, z)|z <-[1..], x <-[1..z],y <-[x..z] ,x^2 + y^2 == z^2] 2 3 auto triples = 4 view::for_each(view::ints(1), [](int z){ 5 return view::for_each(view::ints(1, z), [=](int x){ 6 return view::for_each(view::ints(x, z), [=](int y){ 7 return yield_if(x*x + y*y == z*z, std::make_tuple(x, y, z)); 8 }); 9 }); 10 }); @ü.li:Listing 6: Überladen von Funktionen @li:1 template 2 void advance(I& iter, int n){...} 3 4 template 5 void advance(I& iter, int n){...} 6 7 template 8 void advance(I& iter, int n){...} 9 10 std::list lst{1,2,3,4,5,6,7,8,9}; 11 std::list:: iterator i= lst.begin(); 12 std::advance(i,2); // BidirectionalIterator @ü.li:Listing 7: Abfrage eines std::vector mit std::optional @li:1 #include 2 #include 3 #include 4 5 std::experimental::optional getFirst(const std::vector& vec){ 6 if ( !vec.empty() ) return std::experimental::optional(vec[0]); 7 else return std::experimental::optional(); 8 } 9 10 int main(){ 11 12 std::vector myVec{1,2,3}; 13 std::vector myEmptyVec; 14 15 auto myInt= getFirst(myVec); 16 17 if (myInt){ 18 std::cout << "*myInt: " << *myInt << std::endl; 19 std::cout << "myInt.value(): " << myInt.value() << std::endl; 20 std::cout << "myInt.value_or(2017):" << myInt.value_or(2017) << std::endl; 21 } 22 23 std::cout << std::endl; 24 25 auto myEmptyInt= getFirst(myEmptyVec); 26 27 if (!myEmptyInt){ 28 std::cout << "myEmptyInt.value_or(2017):" << myEmptyInt.value_or(2017) << std::endl; 29 } 30 31 } @ü.li:Listing 8: Erweiterungen von std::future @li:1 future f1= async([]() {return 123;}); 2 future f2 = f1.then([](future f) { 3 return f.get().to_string(); 4 5 }); 6 7 future futures[] = {async([]() { return intResult(125); }), 8 async([]() { return intResult(456); })}; 9 future>> any_f = when_any(begin(futures), end(futures)); 10 11 future futures[] = {async([]() { return intResult(125); }), 12 async([]() { return intResult(456); })}; 13 future>> all_f = when_all(begin(futures), end(futures));