Gra*_*rks 5 c++ boost boost-phoenix
我试图在结构矢量中找到一个元素.以区分大小写的方式搜索时,代码有效.当我尝试将其增强为不区分大小写时,我遇到了两个问题.
简单地说,boost/algorithm/string.hpp打破以前工作的VS2010构建.错误是"'boost :: phoenix :: bind':对重载函数的模糊调用".在Xcode中构建正常.有什么方法可以消除绑定的歧义?
我猜我在第二行(已注释掉)的find_if行中有错误的语法,添加了istarts_with调用.我从凤凰标题中得到错误,说"错误:没有名为'type'的类型".假设问题#1可以修复,我应该如何纠正这一行?
谢谢!
码:
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp> // This include breaks VS2010!
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
using namespace boost::phoenix;
using boost::phoenix::arg_names::arg1;
using boost::istarts_with;
using std::string;
using std::cout;
// Some simple struct I'll build a vector out of
struct Person
{
string FirstName;
string LastName;
Person(string const& f, string const& l) : FirstName(f), LastName(l) {}
};
int main()
{
// Vector to search
std::vector<Person> people;
std::vector<Person>::iterator dude;
// Test data
people.push_back(Person("Fred", "Smith"));
// Works!
dude = std::find_if(people.begin(), people.end(), bind(&Person::FirstName, arg1) == "Fred");
// Won't build - how can I do this case-insensitively?
//dude = std::find_if(people.begin(), people.end(), istarts_with(bind(&Person::FirstName, arg1), "Fred"));
if (dude != people.end())
cout << dude->LastName;
else
cout << "Not found";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您需要两个绑定才能使其工作。首先定义:
int istw(string a, string b) { return istarts_with(a,b); }
Run Code Online (Sandbox Code Playgroud)
然后使用以下内容作为 的谓词find_if:
bind(&istw,bind(&Person::FirstName, arg1),"fred")
Run Code Online (Sandbox Code Playgroud)
两条评论:
bind,即使用boost::phoenix::bind。istw可能是不必要的,但我找不到替换它的正确方法。