Boost logo

Boost Users :

Subject: Re: [Boost-users] [range] count vs count_if
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2012-02-14 14:42:14


Hi Bill,

On Tue, Feb 14, 2012 at 11:26 AM, Bill Buklis <boostusr_at_[hidden]> wrote:
> struct data
> {
>    int a;
>    int b;
> };
>
> // function object that takes two data parameters - contents unimportant for
> the example
> struct match_data;
>
> std::vector<data>    data_range;
> data                            search_value;
>
> boost::find_if( data_range, boost::bind(match_data(), _1, search_value) );

Interesting question. How about:

boost::begin(data_range | filtered(boost::bind(match_data(), _1,
search_value))); ?

HTH,
Nate

P.S. Had to compile it to prove it to myself: (cygwin g++ 4.5.3, old
boost trunk)
$ cat simple.cpp
#include <boost/bind.hpp>
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/begin.hpp>
#define BOOST_TYPEOF_SILENT
#include <boost/typeof/typeof.hpp>
#include <iostream>
#include <vector>

using boost::adaptors::filtered;

struct data
{ int a;
  int b;
};

struct match_data
{
  typedef bool result_type;
  bool operator()(const data& lhs, const data& rhs)
  { return lhs.a == rhs.a; }
};

int main()
{
  std::vector<data> data_range;
  data search_value;
#if 0
  data data1; data1.a = 3;
  data data2; data1.a = 5;
  data_range.push_back(data1);
  data_range.push_back(data2);
  search_value.a = 5;
#endif

  BOOST_AUTO(it, boost::begin(data_range | filtered(
    boost::bind(match_data(), _1, search_value))).base());
  if(it != data_range.end())
    std::cout << "Found: " << it->a;
  else
    std::cout << "Not found.";
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net