C++ (Qt)int main(int argc, char *argv[]){ int myints[] = {16,32,64,128,256,512,1024,2048}; std::vector<int> v(myints,myints + 8); std::vector<int>::iterator low, up; low = std::near_lower(v.begin(), v.end(), 230); // up = std::near_upper(v.begin(), v.end(), 230); // std::cout << "near lower " << (*low) << '\n'; std::cout << "near upper" << (*up) << '\n'; return 0;}
C++ (Qt) int myints[] = {16,32,64,128,256,512,1024,2048}; std::vector<int> v(myints,myints + 8); std::vector<int>::iterator low; int val = 230; if ((low = std::find_if(v.begin(), v.end(), std::bind1st(std::less_equal<int>(), val))) != v.end()) { if (low != v.begin()) --low; std::cout << *low << std::endl; }
C++ (Qt)template<class InputIterator, class T>InputIterator near_lower(InputIterator first, InputIterator last, const T& val){ InputIterator lower; if ((lower = std::find_if(first, last, std::bind1st(std::less_equal<T>(), val))) != last) { if (lower != first) --lower; } return lower;} int main(int argc, char *argv[]){ int myints[] = {16,32,64,128,256,512,1024,2048}; std::vector<int> v(myints,myints + 8); std::vector<int>::iterator low; int val = 230; low = near_lower(v.begin(), v.end(), val); std::cout << "lower " << (*low); return 0;}
C++ (Qt)if ((low = near_lower(v.begin(), v.end(), val) != v.end()) { std::cout << *low << std::endl;}
C++ (Qt)template<class InputIterator, class T>InputIterator near_lower(InputIterator first, InputIterator last, const T& val){ InputIterator lower = first; while (first != last) { if ((val - *first >= 0) && (val - *first <= fabs(val - *lower))) lower = first; ++first; } return lower;} int main(){ std::vector<int> v = {161,-32,64,128,-256,512,1024,2048}; int val = 0; std::vector<int>::iterator low; if ((low = near_lower(v.begin(), v.end(), val)) != v.end()) std::cout << *low << std::endl; return 0;}
C++ (Qt)template<class InputIterator, class T>InputIterator near_lower_s(InputIterator first, InputIterator last, const T& val){ if ((val < *first) || (first == last)) return last; InputIterator lower = std::lower_bound(first, last, val); return (lower == last) ? --lower : lower;}
C++ (Qt)int main(int argc, char *argv[]){ int myints[] = {16,32,64,128,256,512,1024,2048}; std::vector<int> v(myints, myints + sizeof(myints) / sizeof(myints[0])); int resLo, resHi, match = 230; int * bound = std::lower_bound(v.begin(), v.end(), match); if (bound == v.end()) resLo = resHi = v.back(); else { if (*bound == match) resLo = resHi = match; else { resHi = *bound; resLo = (bound == v.begin()) ? resHi : *(bound - 1); } } std::cout << "near lower " << resLo << '\n'; std::cout << "near upper" << resHi << '\n'; return 0;}
C++ (Qt)#include "stdafx.h" #include <iostream>#include <algorithm>#include <vector> template<class InputIterator, class T>T near_lower(const InputIterator first, const InputIterator last, const T& val){ InputIterator lower = std::lower_bound(first, last, val); if ((val <= *first) || (lower == last) || (first == last)) return val; if (val != *lower) --lower; return (*lower);} template<class InputIterator, class T>T near_upper(const InputIterator first, const InputIterator last, const T& val){ InputIterator upper = std::lower_bound(first, last, val); return ((val <= *first) || (upper == last) || (first == last)) ? val : (*upper);} void tst_lower(std::vector<int>::const_iterator &first, std::vector<int>::const_iterator &last){ int test_sequence[] = {1,16,17,230,256,2000,2048,2049,60000}; std::vector<int> tst(test_sequence, test_sequence + 9); std::vector<int>::const_iterator it = tst.begin(); std::vector<int>::const_iterator end = tst.end(); std::cout << std::endl << "Lower test: " << std::endl; for (it; it != end; ++it) { int low = near_lower(first, last, *it); std::cout << "val " << (*it) << ", lower " << low << std::endl; }} void tst_upper(std::vector<int>::const_iterator &first, std::vector<int>::const_iterator &last){ int test_sequence[] = {1,16,17,230,256,2000,2048,2049,60000}; std::vector<int> tst(test_sequence, test_sequence + 9); std::vector<int>::const_iterator it = tst.begin(); std::vector<int>::const_iterator end = tst.end(); std::cout << std::endl << "Upper test: " << std::endl; for (it; it != end; ++it) { int low = near_upper(first, last, *it); std::cout << "val " << (*it) << ", upper " << low << std::endl; }} int _tmain(int argc, _TCHAR* argv[]){ int myints[] = {16,32,64,128,256,512,1024,2048}; std::vector<int> v(myints, myints + 8); tst_lower(v.begin(), v.end()); tst_upper(v.begin(), v.end()); return 0;}
C++ (Qt)typedef std::vector<int>::const_iterator TIter;