close
#include <iostream>
#include <ios>
#include <string>
#include <type_traits>
#include <vector>
#include <algorithm>
using
namespace
std ;
string getname(
int
x)
{
switch
(x)
{
case
0:
return
"zero"
;
case
1:
return
"one"
;
case
2:
return
"two"
;
}
return
"I need to learn to count"
;
}
int
getlength(
const
string& s)
{
return
s.length();
}
template
<
typename
T,
typename
F>
auto execute(
const
T& t, F func) -> decltype(func(t))
{
return
func(t);
}
class
XMAN
{
public
:
bool
operator()(
int
i)
const
{
return
i > 3 && i < 8 && ( i % 2 ) ;
}
};
int
main()
{
cout << execute(
"hello"
, getlength) << endl;
cout << execute(1, getname) << endl;
int
myints[] = {9,8,7,6,5,4,3,2,1};
std::vector<
int
> myvector (myints, myints+9);
auto it = std::find_if( myvector.cbegin(),myvector.cend(),XMAN()) ;
if
( it != myvector.cend() )
cout << *it << endl ;
auto it2 = std::find_if( it+1,myvector.cend(),XMAN()) ;
if
( it != myvector.cend() )
cout << *it2 << endl ;
cout <<
"========================"
<< endl ;
myvector.erase( remove_if(myvector.begin(),myvector.end(),XMAN()),myvector.end() ) ;
for
(auto it = myvector.begin();it != myvector.end();it++)
cout << *it << endl ;
}
全站熱搜
留言列表