5

我试图在结构向量中找到一个元素。该代码在以区分大小写的方式进行搜索时有效。当我尝试将其增强为不区分大小写时,我遇到了两个问题。

  1. 简单地包括boost/algorithm/string.hpp打破以前工作的 VS2010 构建。错误是“'boost::phoenix::bind':对重载函数的模糊调用”。在 Xcode 中构建正常。有什么办法可以消除绑定的歧义吗?

  2. 我想我在第二个(注释掉的)find_if 行中的语法错误,添加了 istarts_with 调用。我从 phoenix 标题中得到错误,说“错误:没有名为 '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;
}
4

1 回答 1

2

您需要两个绑定才能使其工作。首先定义:

int istw(string a, string b) { return istarts_with(a,b); }

然后使用以下作为谓词find_if

bind(&istw,bind(&Person::FirstName, arg1),"fred")

两条评论:

  1. 确保您使用的是正确的bind,即使用boost::phoenix::bind.
  2. 的定义istw可能是不必要的,但我找不到正确的方法来替换它。
于 2011-12-14T09:16:43.367 回答