M
ma740988
Trying to get more acclimated with the use of function objects. As
part of my test, consider:
# include <vector>
# include <iostream>
# include <algorithm>
#include <stdexcept>
#include <bitset>
using std::vector;
using std::cout;
using std::endl;
using std:
stream;
using std::bitset;
class bar {
int iden;
public:
bar(int id) : iden(id) {}
int get_id() const { return iden; }
};
class my_foo {
typedef std::vector <bar> BAR_VEC;
BAR_VEC bv;
public:
my_foo()
{
bv.push_back(bar(0));
bv.push_back(bar(1));
bv.push_back(bar(3));
}
void find ( int id_to_find )
{
BAR_VEC::const_iterator end = bv.end();
for ( BAR_VEC::iterator it = bv.begin(); it != end; ++it )
{
if ( id_to_find == it->get_id() )
{
std::cout << " found " << id_to_find << std::endl;
}
}
// BAR_VEC seg_iter = std::find(
// bv.begin(), bv.end(),
// XXX ); // XXX now the fancy functor or predicate business - i
suppose
}
};
int main()
{
my_foo m;
m.find( 0 ) ;
m.find( 1 ) ;
m.find( 9 ) ;
}
Now within the find function. I'd like to use std::find or
std::find_if to achieve the same result as the for loop but I'm
getting wrapped around the axle trying to 'plug' a parameter/functor
into the third argument of find.
Again this is pure experiment. Perusing a text here and I'm just
trying to create examples as I go along, in an attempt to embrace
different views of iterating across containers/ (where necessary ) call
member functions etc. beyond the convential for loop.
part of my test, consider:
# include <vector>
# include <iostream>
# include <algorithm>
#include <stdexcept>
#include <bitset>
using std::vector;
using std::cout;
using std::endl;
using std:
using std::bitset;
class bar {
int iden;
public:
bar(int id) : iden(id) {}
int get_id() const { return iden; }
};
class my_foo {
typedef std::vector <bar> BAR_VEC;
BAR_VEC bv;
public:
my_foo()
{
bv.push_back(bar(0));
bv.push_back(bar(1));
bv.push_back(bar(3));
}
void find ( int id_to_find )
{
BAR_VEC::const_iterator end = bv.end();
for ( BAR_VEC::iterator it = bv.begin(); it != end; ++it )
{
if ( id_to_find == it->get_id() )
{
std::cout << " found " << id_to_find << std::endl;
}
}
// BAR_VEC seg_iter = std::find(
// bv.begin(), bv.end(),
// XXX ); // XXX now the fancy functor or predicate business - i
suppose
}
};
int main()
{
my_foo m;
m.find( 0 ) ;
m.find( 1 ) ;
m.find( 9 ) ;
}
Now within the find function. I'd like to use std::find or
std::find_if to achieve the same result as the for loop but I'm
getting wrapped around the axle trying to 'plug' a parameter/functor
into the third argument of find.
Again this is pure experiment. Perusing a text here and I'm just
trying to create examples as I go along, in an attempt to embrace
different views of iterating across containers/ (where necessary ) call
member functions etc. beyond the convential for loop.