使用 boost phoenix,如何使用starts_with 调用find_if 调用?

发布于 2024-12-21 04:55:17 字数 1702 浏览 2 评论 0原文

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

  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;
}

I'm trying to find an element in a vector of structs. The code works when searching in a case-sensitive manner. When I try enhancing it to be case-insensitive, I run into two issues.

  1. Simply including boost/algorithm/string.hpp breaks the previously working VS2010 build. The error is "'boost::phoenix::bind' : ambiguous call to overloaded function". Builds OK in Xcode. Any way to disambiguate the bind?

  2. I guess I've got the syntax wrong in the second (commented out) find_if line, adding the istarts_with call. I get errors from the phoenix headers saying "error: no type named 'type'". Assuming issue #1 can be fixed, how should I correct this line?

Thanks!

Code:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

还如梦归 2024-12-28 04:55:17

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

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 的定义可能是不必要的,但我找不到替换它的正确方法。

You'd need two binds to make it work. First define:

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

and then use the following as the predicate for the find_if:

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

Two comments:

  1. Make sure you're using the right bind, namely, use boost::phoenix::bind.
  2. The definition of istw is probably unnecessary but I could not find the right way to replace it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文