C++ (Qt)template <class T>int GetMaxNegative( const T & vec ){ int maxV = 0; for (size_t i = 0; i < vec.size(); ++i) { int val = vec[i]; if (val >= 0) continue; if (!maxV || val > maxV) maxV = val; } return maxV;}
C++ (Qt)int GetMaxNegative( const QWidgetList & vec ){ int maxV = 0; for (size_t i = 0; i < vec.size(); ++i) { int val = vec[i]->x(); if (val >= 0) continue; if (!maxV || val > maxV) maxV = val; } return maxV;}
C++ (Qt)#include <vector>#include <iostream> using namespace std; template<typename Ret, typename It, typename Func>Ret getMaxNegative( It beg, It end, Func get ){ Ret maxV = 0; for( It i = beg; i != end; ++i ) { Ret val = get( *i ); if( val >= 0 ) continue; if( !maxV || val > maxV ) maxV = val; } return maxV;} template<typename Ret, typename It>Ret getMaxNegative( It beg, It end ){ return getMaxNegative<Ret, It>( beg, end, []( Ret v ) { return v; } );} struct Point{ int x; int y;}; int main( int, char ** ){ cout << "Hello World!" << endl; std::vector<int> v{ 3, -1, -14, 1, 5, 9 }; cout << "Result = " << getMaxNegative<int>( v.begin(), v.end() ) << endl; std::vector<Point> p{ { 10, 10 }, { 20, 20 }, { -30, 30 }, { -10, 40 } }; cout << "Result = " << getMaxNegative<double>( p.begin(), p.end(), []( const Point &v ){ return v.x; } ) << endl; return 0;}
C++ (Qt)template <class T>int GetVectorValue(const T & vec, int index) const{ return vec[index];} template <class T>int GetVectorValueByX(const T & vec, int index) const{ return vec[index]->x();} template <class T>int GetVectorValueByY(const T & vec, int index) const{ return vec[index]->y();} // ... blabla template <class T, class F = GetVectorValue>int GetMaxNegative( const T & vec, F* fvec ){ int maxV = 0; for (size_t i = 0; i < vec.size(); ++i) { int val = fvec(vec, i); if (val >= 0) continue; if (!maxV || val > maxV) maxV = val; } return maxV;}
C++ (Qt)template <class>struct getter_helper{ template <class R, class U> static R get(const U & x) { return x; }}; template <>struct getter_helper<QPoint>{ template <class R, class U> static R get(const U & p) { return p.x(); }}; template <class T>int GetMaxNegative( const T & vec){ int maxV = 0; for (size_t i = 0; i < vec.size(); ++i) { int val = getter_helper<typename T::value_type>::get(vec[i]); if (val >= 0) continue; if (!maxV || val > maxV) maxV = val; } return maxV;}
C++ (Qt)struct CWxContainer { CWxContainer( const QWdgetList & lst, QWidget * coord ) : mLst(lst), mCoord(coord) { } size_t size( void ) const { return mLst.size(); } int operator[] ( size_t index ) const { return mCoord->mapFromGlobal(mLst[index]->mapToGlobal(QPoint(0, 0))).x(); } // data const QWdgetList & mLst; QWidget * mCoord;};