c++ - Stl algorithms as function template -
i trying benchmark of stl algorithm on structures :
using namespace std; template<typename a, typename t> void test(){ t c(1000); a(c.begin(), c.end(), true); } int main(){ test<find,vector<bool>>(); }
but template argument deduction/substitution failed
everyone's answer good, let me add note.
to make code work without changing much.. has happen.
#include <vector> #include <algorithm> template< typename searchfunctiontype, typename containertype > void test( searchfunctiontype fsearch, const containertype& container){ fsearch(container.begin(), container.end(), true); } int main(){ std::vector<bool> v(1000); test(std::find<std::vector<bool>::const_iterator, bool>, v); }
std::find not type pointed out everyone. std::find function. may pass function pointer. look, std::find again template function, , compiler has no clue template argument std::find have. that's why don't pass std::find. instead ,
std::find<std::vector<bool>::const_iterator, bool>.
edit:
#include <algorithm> #include <vector> template < typename containertype , typename containertype::const_iterator (*fpsearchfunction) (typename containertype::const_iterator, typename containertype::const_iterator, const typename containertype::value_type&) > void test(const containertype& c, const typename containertype::value_type& value) { fpsearchfunction(c.begin(), c.end(), value); } int main() { std::vector<bool> v(1000); test< std::vector<bool>, std::find<std::vector<bool>::const_iterator, bool> >(v, true); }
this works on visualc. yes. integer type or function pointer can template argument too. bet there better way this.
edit 2:
although don't see why having templetized preferable hard coding, if make complication.
#include <vector> #include <algorithm> #include <chrono> #include <iostream> #include <cstdlib> #include <functional> template <typename containertype> class benchmark { public: typedef typename containertype::const_iterator constiteratortype; typedef typename containertype::value_type valuetype; typedef std::function < constiteratortype(constiteratortype, constiteratortype, const valuetype&) > searchfunctiontype; typedef std::chrono::high_resolution_clock clocktype; void testall(const containertype& container, const valuetype& serachval) { (std::size_t = 0; < m_functions.size(); ++i) { clocktype::time_point begin = clocktype::now(); m_functions[i](container.begin(), container.end(), serachval); clocktype::duration duration = clocktype::now() - begin; std::cout << << " : " << duration.count() << std::endl; } } void pushback(searchfunctiontype fsearch) { m_functions.push_back(fsearch); } private: std::vector<searchfunctiontype> m_functions; }; int main(){ typedef benchmark<std::vector<bool>> benchmarktype; benchmarktype benchmark; benchmark.pushback(std::find<benchmarktype::constiteratortype, benchmarktype::valuetype>); benchmark.pushback([](benchmarktype::constiteratortype begin, benchmarktype::constiteratortype end, const benchmarktype::valuetype& v) { benchmarktype::constiteratortype iter; { iter = begin + std::rand() % (end - begin); if (*iter == v) return iter; } while (iter != end); return end; }); struct binarysearch { benchmarktype::constiteratortype operator()(benchmarktype::constiteratortype begin, benchmarktype::constiteratortype end, const benchmarktype::valuetype& v) { return std::lower_bound(begin, end, v); } }; benchmark.pushback(binarysearch()); std::vector<bool> c; c.assign(10000, false); c.back() = true; benchmark.testall(c, true); }
output:
0 : 100005 1 : 300017 2 : 0
Comments
Post a Comment