D
dave_if
I have a vector of TCard objects, I am sorting them based on the the
box field. That part works fine (finally!). I would then like to
random_shuffle all cards that are in a particular box, that is, I would
find the first card that is in box 3, then the first one after that
which isn't in box 3, and random_shuffle that range instead of the
usual begin(), end(). For the "not in box 3," I kept trying to use the
not1 function but I couldn't get anything that would compile. So I
ended up with (attempt #11):
class FindIfBox
{
public:
FindIfBox(int box): m_box(box) {}
int GetBox() { return m_box; }
bool operator() (const TCard& card) const
private:
int m_box;
};
//...
bool FindIfBox:perator() (const TCard& card) const
{
// will have to be rewritten for Box 0
if (this->m_box >= 0)
return card.m_box == this->m_box;
else
return !(card.m_box == this->m_box);
}
//...
vector<TCard>::iterator boxBegin, boxEnd;
// the sort works perfectly
// not showing InLesserBox function object
stable_sort(v.begin(), v.end(), InLesserBox());
boxBegin = find_if(v.begin(), v.end(), FindIfBox(3));
boxEnd = find_if(boxBegin, v.end(), FindIfBox(-3));
//... (print vector)
// try to shuffle cards with that have box = 3
random_shuffle(boxBegin, boxEnd);
//.. (print vector - and it hasn't changed)
// by the way there are some elements after the box 3 that are in box 4
It took me two weeks to get something that would compile. I obviously
have some logic error because the vector doesn't change after the
shuffle. Maybe I have a temporary going out of scope somewhere, that's
what it usually is, right? Or a missing &.
I wanted to be able to say (and this doesn't compile)
boxEnd = find_if(boxBegin, v.end(), not1(FindIfBox(3));
Is there a way to say something like that using valid C++ so I don't
have to junk up my operator() member function in the FindInBox object?
box field. That part works fine (finally!). I would then like to
random_shuffle all cards that are in a particular box, that is, I would
find the first card that is in box 3, then the first one after that
which isn't in box 3, and random_shuffle that range instead of the
usual begin(), end(). For the "not in box 3," I kept trying to use the
not1 function but I couldn't get anything that would compile. So I
ended up with (attempt #11):
class FindIfBox
{
public:
FindIfBox(int box): m_box(box) {}
int GetBox() { return m_box; }
bool operator() (const TCard& card) const
private:
int m_box;
};
//...
bool FindIfBox:perator() (const TCard& card) const
{
// will have to be rewritten for Box 0
if (this->m_box >= 0)
return card.m_box == this->m_box;
else
return !(card.m_box == this->m_box);
}
//...
vector<TCard>::iterator boxBegin, boxEnd;
// the sort works perfectly
// not showing InLesserBox function object
stable_sort(v.begin(), v.end(), InLesserBox());
boxBegin = find_if(v.begin(), v.end(), FindIfBox(3));
boxEnd = find_if(boxBegin, v.end(), FindIfBox(-3));
//... (print vector)
// try to shuffle cards with that have box = 3
random_shuffle(boxBegin, boxEnd);
//.. (print vector - and it hasn't changed)
// by the way there are some elements after the box 3 that are in box 4
It took me two weeks to get something that would compile. I obviously
have some logic error because the vector doesn't change after the
shuffle. Maybe I have a temporary going out of scope somewhere, that's
what it usually is, right? Or a missing &.
I wanted to be able to say (and this doesn't compile)
boxEnd = find_if(boxBegin, v.end(), not1(FindIfBox(3));
Is there a way to say something like that using valid C++ so I don't
have to junk up my operator() member function in the FindInBox object?