5

構造体のベクトルから要素を見つけようとしています。このコードは、大文字と小文字を区別して検索するときに機能します。大文字と小文字を区別しないように拡張しようとすると、2つの問題が発生します。

  1. 単に含めるboost/algorithm/string.hppと、以前に機能していたVS2010ビルドが中断されます。エラーは「'boost:: phoenix :: bind':オーバーロードされた関数へのあいまいな呼び出し」です。XcodeでOKをビルドします。バインドを明確にする方法はありますか?

  2. 2番目の(コメントアウトされた)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;
}
4

1 に答える 1

2

それを機能させるには、2 つのバインドが必要です。最初に次を定義します。

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

の述語として次を使用しますfind_if

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

2 つのコメント:

  1. 正しいbind、つまり useを使用していることを確認してくださいboost::phoenix::bind
  2. の定義istwはおそらく不要ですが、それを置き換える正しい方法が見つかりませんでした。
于 2011-12-14T09:16:43.367 に答える