读书人

求教关于bind1st用来绑定vector:pus

发布时间: 2012-09-24 13:49:42 作者: rapoo

求教,关于bind1st用来绑定vector::push_back等stl函数的问题
还是研究了一下bind1st,bind2nd,mem_fun系列函数发现有一个问题,对于我自己写的void T::print(int),在仿函数化后bind1st能正常绑定,但对于诸如vector<>::push_back(int)此类函数,似乎无法正常工作,请问这是怎么回事?
D:\Programing\codes\temp\main.cpp|27|instantiated from here|

d:\programing\codeblocks\files\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\backward\binders.h|117|error: 'typename _Operation::result_type std::binder1st<_Operation>::operator()(typename _Operation::second_argument_type&) const [with _Operation = std::mem_fun1_ref_t<void, std::vector<int, std::allocator<int> >, const int&>]' cannot be overloaded|

d:\programing\codeblocks\files\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\backward\binders.h|111|error: with 'typename _Operation::result_type std::binder1st<_Operation>::operator()(const typename _Operation::second_argument_type&) const [with _Operation = std::mem_fun1_ref_t<void, std::vector<int, std::allocator<int> >, const int&>]'|

||=== 已完成构建: 2 个错误, 0 个警告 ===|

C/C++ code
#include <iostream>#include <vector>#include <functional>#include <algorithm>using namespace std;class T{    public:    void print(int num)    {        cout<<num<<endl;    }};int main(){    T t;    vector<int> vec(10);    bind1st(            mem_fun_ref(                        &T::print),t);//bind1st对T::print正常工作    bind1st(            mem_fun_ref(                        &vector<int>::push_back),vec);//!!!!bind1st对vector<>::push_back错误工作,说binder1st不可重载什么的            mem_fun_ref(                        &vector<int>::push_back)(vec,1);//mem_fun_ref对vector<>::push_back正常工作                        cout<<vec.back();    vector<T> vec2(10);    for_each(vec2.begin(),vec2.end(),             bind2nd(                     mem_fun_ref(&T::print),2));//bind2nd对print正常工作    vector<vector<int> > vec3(10);    for_each(vec3.begin(),vec3.end(),             bind2nd(                     mem_fun_ref(&vector<int>::push_back),1));bind2nd对vector<>::push_back正常工作    cout<<vec3.at(10).back()<<endl;    return 0;}


[解决办法]
VS的BUG , 不用就是.

读书人网 >C++

热点推荐