Boost 1.50.0ã§ãBoost Algorithmã©ã¤ãã©ãªãæ°ãã«è¿½å ããã¾ãããããããããã¤ãç´¹ä»ãã¦ãããã¨æãã¾ãã
ä»åã¯clamp()ã§ãã
clamp()ã¯ãæå°å¤ã¨æ大å¤ãè¨å®ããä¸ã§å¤ã渡ãã¨ããã®ç¯å²å
ã«å¤ãåãã¦ãããé¢æ°ã§ãã
æå³çã«ã¯ãmin(max(minval, x), maxval)ã¨åãã«ãªãã¾ãã
以ä¸ã®ä¾ã§ã¯ãå¤ã0ãã10ã®ç¯å²å¤ã ã£ãããã®ç¯å²ã«åãããã¨ãããã¨ããã¦ãã¾ãã
#include <iostream> #include <boost/algorithm/clamp.hpp> int main() { using boost::algorithm::clamp; int x = 11; x = clamp(x, 0, 10); int y = -1; y = clamp(y, 0, 10); std::cout << x << std::endl; std::cout << y << std::endl; }
10 0
ã¾ããclamp()ã«ããå¤ã®å¤æå¦çããå¤ã®Rangeã«å¯¾ãã¦è¡ãclamp_range()é¢æ°ãããã¾ãã
ãã®é¢æ°ã¯ãOutputIteratorã§å¤æå¾ã®Rangeãè¿ãã¾ãã
#include <iostream> #include <vector> #include <boost/algorithm/clamp.hpp> int main() { std::vector<int> v = {-1, 2, 5, 8, 11}; // Rangeã®å¤ã0ãã10ã®ç¯å²ã«åãã boost::algorithm::clamp_range(v, v.begin(), 0, 10); for (int x : v) { std::cout << x << std::endl; } }
0 2 5 8 10
åç
§ï¼
clamp - Boost Algorithm Library
clamp関数の書き方