- 论坛徽章:
- 0
|
〇〇 发表于 2012-09-10 19:13 ![]()
你加个main函数和#include
这个:- #include <cstddef>
- #include <iostream>
- namespace one_of_private
- {
- template< typename Predicate, typename First_Input_Iterator, typename ... Rest_Input_Iterators >
- std::size_t _one_of_count( Predicate predict, First_Input_Iterator first_, First_Input_Iterator last_, Rest_Input_Iterators ... rest_ )
- {
- if ( first_ == last_ ) return 0;
- return ( predict( *first_++, *rest_++...) ? 1 : 0 ) +
- _one_of_count( predict, first_, last_, rest_...);
- }
- struct dummy{};
- template<typename Predicate, typename ... Input_Iterators>
- std::size_t rotate_one_of_count_impl( Predicate predict, dummy, Input_Iterators ... inputs )
- {
- return _one_of_count( predict, inputs ... );
- }
-
- template<typename Anonymous_Arg1, typename ... Anonymous_Argn>
- std::size_t rotate_one_of_count_impl( Anonymous_Arg1 arg1, Anonymous_Argn ... argn )
- {
- return rotate_one_of_count_impl( argn ..., arg1 );
- }
- }//namespace one_of_private
- //example:
- // int a[4] = { 1, 2, 3, 4 };
- // int A[4] = { 2, 4, 6, 8 };
- // bool b = one_of( a, a+4, A, [](int a_, int A_){ return a_+a_ == A_;} );
- template<typename ... Input_Iterators_and_Predict>
- bool one_of( Input_Iterators_and_Predict ... all_args )
- {
- static_assert( sizeof ... ( all_args ) > 2, "one_of requires at least 3 arguments" );
- return 1 == one_of_private::rotate_one_of_count_impl( all_args ..., one_of_private::dummy() );
- }
- int main()
- {
- int a[4] = { 1, 2, 3, 4 };
- int b[4] = { 2, 3, 4, 5 };
- int c[4] = { 3, 4, 5, 6 };
- int d[4] = { 4, 5, 6, 8 };
- bool bo = one_of( a, a+4, b, c, d, []( int a_, int b_, int c_, int d_ ) { return b_+c_ != a_ + d_; } );
- if ( bo ) std::cout << "true\n";
- else std::cout << "false\n";
- return 0;
- }
复制代码 谢了 |
|