-
-
Save drewxa/de63d3bd021029208efb70ab146ac7b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <iomanip> | |
#include <cstdint> | |
#include <array> | |
using size_t = std::size_t; | |
// тут не шаблон, а auto в аргементах | |
auto simple_mult = [](const auto& a, const auto& b){return a*b;}; | |
template <class T, class F = decltype(simple_mult)> | |
auto fast_pow(const T& a, size_t degree, F mult = simple_mult) // и тут <T> не нужен | |
{ | |
auto power = a; | |
auto result = T(1); | |
while (degree){ | |
if (degree & 1){ | |
result = mult(result, power); | |
} | |
power = mult(power, power); | |
// а тут degree не изменялся | |
degree = degree >> 1; | |
} | |
return result; | |
} | |
int main() { | |
std::cout << "Hello, World!" << std::endl; | |
std::cout << fast_pow(2, 3) << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment