65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
/*
typedef (const string&) constStringRef;
typedef (const string ) constString;
//typedef string& StringRef;
*/
///------------------------------------------------
class Test
{
public:
Test(int a, const string& b)
:_a(a), _b(b){ }
~Test() {}
bool compareByInt( const int a){
return a == _a;
}
bool compareByString(const string b){
return b == _b;
}
bool compareByStringRef(const string& b){ <-------- const string& b
return b == _b;
}
int _a;
string _b;
void print(){
cout << "<" << _a << "," << _b << ">, " ;
}
};
void print();
///------------------------------------------------
vector < Test > vt;
///------------------------------------------------
int main()
{
vt.push_back(Test(1, "A"));
vt.push_back(Test(2, "B"));
vt.push_back(Test(3, "C"));
vt.push_back(Test(4, "D"));
print();
//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const int>(&Test::compareByInt)
, 2 )
),
vt.end());
print();
//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const string>(&Test::compareByString)
, "D" )
),
vt.end());
print();
/*---编译错误-----------------------------------
1>...\....\functional(312) : error C2529: '_Right' : reference to reference is illegal
我想使用compareByStringRef(const string& b),因为这比compareByString(const string b)效率高,
可是无法编译,怎么办? 难道是应该用mem_funX_XXX系列中的其他几个?
*/
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun<bool,Test,const string& >(&Test::compareByStringRef)
, "A" )
),
vt.end());
print();
return 1;
}
///------------------------------------------------
void print()
{
vector < Test > ::iterator i = vt.begin();
vector < Test > ::iterator e = vt.end();
for(; i!=e; ++i){
i->print();
}
cout << endl;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
/*
typedef (const string&) constStringRef;
typedef (const string ) constString;
//typedef string& StringRef;
*/
///------------------------------------------------
class Test
{
public:
Test(int a, const string& b)
:_a(a), _b(b){ }
~Test() {}
bool compareByInt( const int a){
return a == _a;
}
bool compareByString(const string b){
return b == _b;
}
bool compareByStringRef(const string& b){ //<-------- const string& b
return b == _b;
}
int _a;
string _b;
void print(){
cout << "<" << _a << "," << _b << ">, " ;
}
};
void print();
///------------------------------------------------
vector < Test > vt;
///------------------------------------------------
int main()
{
vt.push_back(Test(1, "A"));
vt.push_back(Test(2, "B"));
vt.push_back(Test(3, "C"));
vt.push_back(Test(4, "D"));
print();
//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd(
mem_fun1_ref<bool,Test, const int>(&Test::compareByInt)
, 2 )
),
vt.end());
print();
//--运行OK------------------------------------
vt.erase( remove_if(vt.begin(), vt.end(),
bind2nd( mem_fun1_ref<bool,Test, const string>(&Test::compareByString)
, "D" )
),
vt.end());
print();
vt.erase( remove_if(vt.begin(),vt.end(),
bind2nd( mem_fun_ref<bool,Test,const string& >(&Test::compareByStringRef)
, "A" ) ), vt.end());
print();
return 1;
}
///------------------------------------------------
void print()
{
vector < Test > ::iterator i = vt.begin();
vector < Test > ::iterator e = vt.end();
for(; i!=e; ++i){
i->print();
}
cout << endl;
}
int &&x; // 引用的引用不合法,必须想办法去掉一个&
// It's ok
mem_fun_ref<bool,Test,const string& >(&Test::compareByStringRef)(Test(11, "A"), "A");
vt.erase( remove_if(vt.begin(), vt.end(),
boost::bind( &Test::compareByStringRef, _1, "A" )),
vt.end());