Skip to content

Instantly share code, notes, and snippets.

@martinmoene
Created August 23, 2015 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinmoene/ec85022ce720852125c4 to your computer and use it in GitHub Desktop.
Save martinmoene/ec85022ce720852125c4 to your computer and use it in GitHub Desktop.
C++ example of proposed clamp() used with range-v3 library.
// clamp-range-v3
#include <iostream>
#include <clamp.hpp>
#include <range/v3/core.hpp>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/remove_if.hpp>
#include <range/v3/view/transform.hpp>
struct is_odd
{
bool operator()(int i) const
{
return (i % 2) == 1;
}
};
int main()
{
using namespace ranges;
auto && rng =
view::iota(0, +20) |
view::remove_if( is_odd() ) |
view::transform( [](int x) { return clamp(x, 5, 15); } );
std::cout << rng;
}
#if 0
g++ -std=c++11 -I"../clamp (Dropbox)" -I../range-v3/include -I.. -o clamp-range-v3.exe clamp-range-v3.cpp && clamp-range-v3.exe
[5,5,5,6,8,10,12,14,15,15]
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment