今天调试程序的时候,遇到了这样一个问题
bool check(int elem);
vecot<int>v; ...
pos=find_if(v.begin(),v.end(),not1(check))竟然出错,查找资料之后,发现,原来原因如下:
ptr_fun做的唯一的事是使一些typedef有效( 仿函数类的operator()所带的参数的类型和它的返回类型。对于binary_function,你要指定三个类型:你的operator的第一个和第二个参数的类型,和你的operator地返回类型; 而这两个基类 typedef 了argument_type、first_argument_type、second_argument_type和result_type 这几种类型
)。就是这样。not1需要这些typedef,这就是为什么可以把not1应用于ptr_fun,但不能直接对check应用not1。因为是低级的函数指针,check缺乏not1需要的typedef。
not1不是STL中唯一有那些要求的组件。四个标准函数适配器(not1、not2、bind1st和bind2nd)都需要存在某些typedef
c++标准程序库提供的一些结构如下:
template<typename Arg,typename Result>
struct unary_function{
typedef Arg argument_type;
typedef Result result_type;
};
template<typename Arg1,typename Arg2,typename Result>
struct binary_function{
typedef Arg1 first_argument_type;
typedef Arg2 second_argument_type;
typedef Result result_type;
};
not2也是一样的道理,not2是用于一个含有两个参数返回值为bool的函数
pos=find_if(v.begin(),v.end(),not1(ptr_fun(check)));这样是正确的。
#include<iostream>
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
using namespace std;
bool check(int a){
if(a%2==0){
return true;
}else{
return false;
}
}
int main(){
int a[]={1,2,3,4,5,6,7,8,9,10};
vector<int>v(a,a+10);
vector<int>::iterator it=find_if(v.begin(),v.end(),not1(ptr_fun(check)));
cout<<*it<<endl;
system("pause");
return 0;
}
mem_fun_ref和men_fun是针对成员函数而设计的函数适配器
class Person{
public:
void print() const{
...
}
}
int main(){
vector<Person>v;
...
for_each(v.begin(),v.end(),men_fun_ref(&Person::print));//调用成员数
}
class Person{
public:
void print() const{
...
}
}
int main(){
vector<Person*>v;
...
for_each(v.begin(),v.end(),men_fun(&Person::print));//调用成员数
}
知道差别了吗
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<cmath>
using namespace std;
template<typename T1,typename T2>
struct fopow:public binary_function<T1,T2,T1>
{
T1 operator()(T1 base,T2 exp) const{
return pow(base,exp);
}
};
int main(){
vector<int>v;
for(int i=1;i<=9;++i){
v.push_back(i);
}
transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind1st(fopow<float,int>(),3));
cout<<endl;
transform(v.begin(),v.end(),ostream_iterator<int>(cout," "),bind2nd(fopow<float,int>(),3));
cout<<endl;
system("pause");
return 0;
}
分享到:
相关推荐
boost库中,mem_fun使用实例代码, 注意要有boost库才能编译. 用于将成员函数转化为函数指针.
mem_cpu.rar mem_cpu.rar mem_cpu.rar mem_cpu.rar
MEM_BAD_POINTER 错误解决办法.txt
if(HAL_I2C_Mem_Read(&_MLX90615_I2C_MLX90615_I2C_ADDRESS0x27I2C_MEMADD_SIZE_8BITData3100)!=HAL_OK)return false;double tempFactor = 0.02; double tempData = 0x0000; tempData = (double)(((Data[1] &...
详细介绍了利用request_mem_region管理I/O内存映射列表的过程,绝对值得一看!!
采用verilog实现RTLAB多路驱动程序
实现自己的mem_malloc和mem_free
Rose关闭错误MEM_BAD_POINTER_EMEND补丁。
Rose MEM_BAD_POINTER 错误解决办法.txt
python库。 资源全名:mem_usage_ui-0.1.tar.gz
实现I2C正确读写AT24C02,基于STM32 CUBE,读写使用HAL_I2C_Mem_Write,HAL_I2C_Mem_Read,写函数需单个字节写入。
专门针对fsc100使用的内存进行初始化,拷贝在board/samsung/fsc100 详见ARM_VvN博客
linux_os_mem_cpu_usage.htm
MEM_MVM_MNM_MUSIC_以及前后向平滑技术在两个相干源下的性能对比仿真代码
虚拟内存管理,用最先适配法,最优适配法,最差适配法
简单内存池
SHW32.DLL 博文链接:https://bluestar.iteye.com/blog/161496
DWD_WR_CAMP_MEM_INFO.sql
这是 我修改的 test_mem_ctrl.v文件, 控制 lattice ddr_ip 的代码。
一个C语言写简单的内存池,模仿并简化了mpool2.1.0的实现,供学习参考。