- 论坛徽章:
- 0
|
本帖最后由 kingoftime3 于 2013-04-26 22:53 编辑
- #include <list>
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <string>
- using namespace std;
- template<class T>
- struct IsSame : binary_function<pair<string,string>, T, bool>
- {
- bool operator()(const pair<string,string> &p, const T &s) const
- {
- return p.first==s;
- }
- };
- static void print(const pair<string, string> &p)
- {
- cout << p.first << "\t" << p.second << endl;
- }
- int main()
- {
- list<pair<string,string> > mlist;
- mlist.push_back(pair<string,string>("aaa", "aaa"));
- mlist.push_back(pair<string,string>("aaa1", "aaa"));
- mlist.push_back(pair<string,string>("aaa2", "aaa"));
- mlist.push_back(pair<string,string>("aaa3", "aaa"));
- mlist.remove_if(binder2nd<IsSame<string> >(IsSame<string>(), "aaa"));
- for_each(mlist.begin(), mlist.end(), print);
- system("pause");
- return 0;
- }
复制代码 如果- bool operator()(const pair<string,string> &p, const T &s) const
复制代码 写成- bool operator()(const pair<string,string> &p, const T &s)
复制代码 就会报错- 1>d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(329): error C3848: 具有类型“const IsSame<T>”的表达式会丢失一些 const-volatile 限定符以调用“bool IsSame<T>::operator ()(const std::pair<_Ty1,_Ty2> &,const T &)”
- 1> with
- 1> [
- 1> T=std::string
- 1> ]
- 1> d:\program files\microsoft visual studio 11.0\vc\include\xfunctional(328): 编译类 模板 成员函数“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”时
- 1> with
- 1> [
- 1> _Fn2=IsSame<std::string>,
- 1> _Ty1=std::string,
- 1> _Ty2=std::string
- 1> ]
- 1> d:\program files\microsoft visual studio 11.0\vc\include\list(1615): 参见对正在编译的函数 模板 实例化“bool std::binder2nd<_Fn2>::operator ()(std::pair<_Ty1,_Ty2> &) const”的引用
- 1> with
- 1> [
- 1> _Fn2=IsSame<std::string>,
- 1> _Ty1=std::string,
- 1> _Ty2=std::string
- 1> ]
- 1> c:\users\chenxie\documents\visual studio 2012\projects\simpletest\simpletest\simpletest.cpp(30): 参见对正在编译的类 模板 实例化“std::binder2nd<_Fn2>”的引用
- 1> with
- 1> [
- 1> _Fn2=IsSame<std::string>
- 1> ]
复制代码 |
|