構造体のベクトルから要素を見つけようとしています。このコードは、大文字と小文字を区別して検索するときに機能します。大文字と小文字を区別しないように拡張しようとすると、2つの問題が発生します。
単に含める
boost/algorithm/string.hpp
と、以前に機能していたVS2010ビルドが中断されます。エラーは「'boost:: phoenix :: bind':オーバーロードされた関数へのあいまいな呼び出し」です。XcodeでOKをビルドします。バインドを明確にする方法はありますか?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;
}