D
Dilip
I have a vector of class object pointers that have 2 internal states
(USED or EMPTY). USED is just a way of indicating the element in that
slot has valid state. EMPTY means the element is available for re-use.
I was trying to write some code to locate an used element using find_if
with a custom predicate derived from unary_function. That was pretty
easy. I however wanted something else. If the element cannot be
located, I wanted to return the index of the first (or for that matter
*any*) empty slot. I am trying to do this in one pass without having
to write another predicate to test for empty slots. I was about to
write something like this:
struct no_op
{
string my_name;
no_op() : my_name("default_name") { }
no_op(string _my_name) : my_name(_my_name) { }
void cleanthyself() { my_name = "EMPTY_SLOT"; }
};
typedef vector<no_op*> vecNoOps;
struct oplocator : public binary_function<no_op, short, bool>
{
string _name;
explicit oplocator(const string& name) : _name(name) { }
bool operator()(const no_op* opobj, short& emptyslotIdx) const
{
static int i = 0;
++i;
if (opobj->my_name == _name) return true;
if (opobj->my_name == "EMPTY_SLOT") { emptyslotIdx = i; }
return false;
}
};
I thought on the client side I will just try to locate the element I am
interested in as usual and if I reach the end of the vector without
locating I will atleast have the index of the empty slot. I also
thought I'd use some kind of default value for emptyslotIdx to tell if
I was able to found an emptyslot at all. If nothing is available I
will simply create a new object.
After reading Meyers' Effective STL item 39 I am no longer sure. He
wants developers to avoid exactly this kind of programming (maintaining
a local static).
Is there a way to do what I want without having to make 2 passes at the
vector of no_ops?
thanks!
(USED or EMPTY). USED is just a way of indicating the element in that
slot has valid state. EMPTY means the element is available for re-use.
I was trying to write some code to locate an used element using find_if
with a custom predicate derived from unary_function. That was pretty
easy. I however wanted something else. If the element cannot be
located, I wanted to return the index of the first (or for that matter
*any*) empty slot. I am trying to do this in one pass without having
to write another predicate to test for empty slots. I was about to
write something like this:
struct no_op
{
string my_name;
no_op() : my_name("default_name") { }
no_op(string _my_name) : my_name(_my_name) { }
void cleanthyself() { my_name = "EMPTY_SLOT"; }
};
typedef vector<no_op*> vecNoOps;
struct oplocator : public binary_function<no_op, short, bool>
{
string _name;
explicit oplocator(const string& name) : _name(name) { }
bool operator()(const no_op* opobj, short& emptyslotIdx) const
{
static int i = 0;
++i;
if (opobj->my_name == _name) return true;
if (opobj->my_name == "EMPTY_SLOT") { emptyslotIdx = i; }
return false;
}
};
I thought on the client side I will just try to locate the element I am
interested in as usual and if I reach the end of the vector without
locating I will atleast have the index of the empty slot. I also
thought I'd use some kind of default value for emptyslotIdx to tell if
I was able to found an emptyslot at all. If nothing is available I
will simply create a new object.
After reading Meyers' Effective STL item 39 I am no longer sure. He
wants developers to avoid exactly this kind of programming (maintaining
a local static).
Is there a way to do what I want without having to make 2 passes at the
vector of no_ops?
thanks!