1
int nums[] = {7, 6, 12, 9, 29, 1, 67, 3, 3, 8, 9, 77};
std::vector<int> vecInts(&nums[0], &nums[0] + sizeof(nums)/sizeof(nums[0]));
int countBoost = 0;
// (i > 5 && i <=10)
countBoost = std::count_if(vecInts.begin(), vecInts.end(),
boost::bind(std::logical_and<bool>(),
boost::bind(std::greater<int>(), _1, 5),
boost::bind(std::less_equal<int>(), _1, 10))
);
ここでは、純粋なSTLと同じ論理を実装する必要があります。どうやってやるの?std :: logical_andを使用して2つの条件を結合する
私は、次のコードを試してみました、それが動作しません:
int countSTL = std::count_if(vecInts.begin(), vecInts.end(),
std::logical_and<bool>(std::bind2nd(std::greater<int>(), 5), std::bind2nd(std::less_equal<int>(), 10))
);
は
//更新//
In Effective STL Item 43, Meyers indicates as follows:
vector<int>::iterator i = find_if(v.begin(), v.end(),
compose2(logical_and<bool>(), bind2nd(greater<int>(), x),
bind2nd(less<int>(), y)));
しかしcompose2標準ではありませんありがとう関数オブジェクトアダプタ。 「純粋な」C++ 03のstdで
さて、 'boost :: bind'を' std :: bind'に変更してみることができます。 –
std :: bindは新しい関数をC++ 11にバインドしていますか?可能であれば、 'std :: bind1st'や' std :: bind2nd'を実装する方法を見たいと思います。 – q0987
C++ 03では、 'std :: bind1st'、' std :: bind2nd'と 'std :: compose2'を使います。例については、[here](http://www.sgi.com/tech/stl/logical_and.html)を参照してください。 –