#include <iostream>
#include <vector>
namespace std17
{
template<class InputIt>
typename std::iterator_traits<InputIt>::value_type
reduce(InputIt first, InputIt last)
{
std::iterator_traits<InputIt>::value_type sum =
std::iterator_traits<InputIt>::value_type();
for (auto iter = first; iter != last; iter++)
{
sum = sum + *iter;
}
return sum;
}
}
int main()
{
std::vector<int> myInputVector{1, 2, 3, 4, 5};
int result;
result = std17::reduce(myInputVector.begin() ,
myInputVector.end() );
std::cout << result << " ";
std::cout << std::endl;
return 0;
}
// Output: 15
Reference: http://en.cppreference.com/w/cpp/algorithm/reduce
Monday, November 6, 2017
C++17: std::reduce()
C++17 added std::reduce(), which applies applies a binary operation on a range of elements. The default binary operation is addition. Here is an example (simulated with C++14):
C++17: New Headers
C++17 added the following headers:
<any>
<execution>
<memory_resource>
<optional>
<string_view>
<variant>
Reference: http://en.cppreference.com/w/cpp/header
C++: SFINAE
SFINAE stands for “Substitution Failure
is Not an Error”. When deducing which template function to use, if the compiler
tries one template function, and if the substitution of template parameters is
a problem; instead of the compiler declaring an error and stopping, the
compiler tries the remaining template functions.
References:
C++17: std::gcd() and std::lcm()
C++17 added the greatest common divisor (GCD) function and the least common multiple (LCM) function.
http://en.cppreference.com/w/cpp/numeric/gcd
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0295r0.pdf
#include
#include
int main()
{
std::cout << std::gcd( 0, 10) << " "; // 10
std::cout << std::gcd( 1, 10) << " "; // 1
std::cout << std::gcd( 2, 10) << " "; // 2
std::cout << std::gcd( 3, 10) << " "; // 1
std::cout << std::gcd( 4, 10) << " "; // 2
std::cout << std::gcd( 5, 10) << " "; // 5
std::cout << std::gcd( 6, 10) << " "; // 2
std::cout << std::gcd( 7, 10) << " "; // 1
std::cout << std::gcd( 8, 10) << " "; // 2
std::cout << std::gcd( 9, 10) << " "; // 1
std::cout << std::gcd(10, 10) << " "; // 10
std::cout << ": ";
std::cout << std::lcm( 0, 10) << " "; // 0
std::cout << std::lcm( 1, 10) << " "; // 10
std::cout << std::lcm( 2, 10) << " "; // 10
std::cout << std::lcm( 3, 10) << " "; // 30
std::cout << std::lcm( 4, 10) << " "; // 20
std::cout << std::lcm( 5, 10) << " "; // 10
std::cout << std::lcm( 6, 10) << " "; // 30
std::cout << std::lcm( 7, 10) << " "; // 70
std::cout << std::lcm( 8, 10) << " "; // 40
std::cout << std::lcm( 9, 10) << " "; // 90
std::cout << std::lcm(10, 10) << " "; // 10
std::cout << std::endl;
return 0;
}
// Output: 10 1 2 1 2 5 2 1 2 1 10 : 0 10 10 30 20 10 30 70 40 90 10
References:http://en.cppreference.com/w/cpp/numeric/gcd
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0295r0.pdf
C++17: C++17 is based on C11 instead of C99.
C++17 is based on C11 instead of C99.
C++17: std::clamp()
C++17 added the template function std::clamp() is a combination of std::max() and std::min(). Here is an example:
#include <algorithm>
#include <iostream>
int main()
{
// Value Min Max
std::cout << std::clamp( 5 , 0, 10) << " "; // 5
std::cout << std::clamp(-5 , 0, 10) << " "; // 0
std::cout << std::clamp(20 , 0, 10) << " "; // 10
std::cout << std::endl;
return 0;
}
// Output: 5 0 10
Reference: https://stackoverflow.com/questions/38060436/what-are-the-new-features-in-c17
C++17: The register keyword was removed.
C++17 removed the register keyword. This
keyword used to be used to hint to the compiler that a local variable is used
so much that it would be nice to use a CPU Register for it instead of the
stack.
Subscribe to:
Posts (Atom)
