如何绑定模板函数
how to bind a template function
大家好!
我想在一个字符串向量中找到一个特定的元素,忽略大小写:
#include <iostream>
#include <string>
#include <vector>
#include "boost/algorithm/string.hpp"
#include "boost/bind.hpp"
using std::string;
using std::vector;
bool icmp(const string& str1, const string& str2)
{
return boost::iequals(str1, str2);
}
int main(int argc, char* argv[])
{
vector<string> vec;
vec.push_back("test");
// if (std::find_if(vec.begin(), vec.end(), boost::bind(&boost::iequals<string,string>, "TEST", _1)) != vec.end()) <-- does not compile
if (std::find_if(vec.begin(), vec.end(), boost::bind(&icmp, "TEST", _1)) != vec.end())
std::cout << "found" << std::endl;
return 0;
}
到目前为止,这是正常工作的,但我想知道的是,如果有可能摆脱额外的函数(icmp())并直接调用equals(模板函数)(就像在注释行中一样)。
提前感谢!
添加模板参数和默认locale参数在我的机器上有效。
if (std::find_if(vec.begin(), vec.end(), boost::bind(&boost::iequals<string,string>, "TEST", _1, std::locale())) != vec.end())
std::cout << "found" << std::endl;
我相信这不是你所希望的,但这似乎是我唯一能解决的问题
(使用g++ c++03模式):
typedef bool (*fptr)(const std::string&, const std::string&);
if (std::find_if(vec.begin(), vec.end(),
boost::bind((fptr) &boost::iequals<string,string>, "TEST", _1)
) != vec.end())
使用boost lambda:)
#include <iostream>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
#include <vector>
using namespace std;
using namespace boost::lambda;
int main()
{
vector<string> vec;
vec.push_back("TEST");
vec.push_back("test");
vector<string>::iterator it;
it = find_if(vec.begin(),vec.end(),_1 == "test");
cout << *it << endl;
return 0;
}
相关文章:
- 将自由函数绑定为类成员函数
- 有没有办法将重载的类函数绑定到函数对象?
- 如何将类 1 的 std::函数绑定到类 2 的函数?
- 如何将 STL 队列推送函数绑定到 std::函数?
- 如何在 C# 中将带有参数的函数绑定到包中
- 如何仅使用 c/python API 将 c++ 成员函数绑定到 python?
- 将非静态函数绑定到回调时出现问题
- 意外输出..函数绑定在虚拟表中的发生方式
- 将类实例函数绑定到 v8::FunctionTemplate
- 是否可以将匿名 lambda 函数绑定到对象以允许 lambda 中的代码访问对象的成员?
- 使用JNI将C 类成员函数绑定到Java
- 将静态成员函数绑定为回调
- 如何使用 boost::bind 将静态成员函数绑定到 boost::function
- 如何将C++函数绑定到xaml文本块属性
- boost::使用成员函数绑定回调的帮助
- 如何将成员函数绑定到 C++14 中的对象
- std::函数绑定到成员函数
- 将带有unique_ptr参数的函数绑定到 std::function<void()>
- 将常规函数绑定到 std::function
- std::将成员函数绑定到 nullptr 处的实例,导致看似随机的 this 指针