I'm working on an assignment for school where my teacher wants me to convert each member of an array of a class if it's int member is greater than 300. I can make it work for the first value of 300, since that's what find_if returns. But how do I make it work for all members? Here's some code:
#include "Test.h"
#include "Drawer.h"
#include <iostream>
using namespace std;
#include <Algorithm>
CDrawer gCanvas(RGB(0,0,0));
void Draw(CTest [], int);
bool myCountIf(CTest const &);
bool myCountIf2(CTest const &);
// ICA 19 ** Fill in the required blocks, labels and pauses are supplied
int main( void )
{
CTest a[150];
CTest b[150];
CTest c[150];
for (int i(0); i < 150; ++i)
{
a[i] = CTest(300);
b[i]= CTest((i + 1) * 4);
c[i] =CTest(rand() % 600);
}
cout << "a - Fixed 300\n";
Draw( a, 150 );
cin.get();
cout << "b - Increment\n";
Draw( b, 150 );
cin.get();
cout << "c - Random\n";
Draw( c, 150 );
cin.get();
// Min and Max of c here
cout << "Min of c : " << min_element(c, c + 150)->GetY() << endl;
cout << "Max of c : " << max_element(c, c + 150)->GetY() << endl;
cin.get();
// Count of 200, Count > 300 here
cout << "Count of b == 200 : " << count_if(b, b + 150, myCountIf) << endl;
cout << "Count of b > 300 : " << count_if(b, b + 150, myCountIf2) << endl;
cin.get();
// Find and Set() 200 in b
cout << "Found 200 in b, setting to RED\n";
find_if(b, b + 150, myCountIf)->Set();
Draw( b, 150 );
cin.get();
// Find and Set() all > 300 in c here ***************************
//for_each doesnt seem to work here
cout << "Found > 300 in c, setting to RED\n";
for_each(c, b + 150, find_if(b, b + 150, myCountIf)->Set());
Draw( c, 150 );
cin.get();
return 0;
}
void Draw(CTest Array[], int iCount)
{
gCanvas.Clear();
for (int i(0); i < iCount; ++i)
gCanvas.AddLine(i * 5, 600, i * 5, 600 - Array[i].GetY(),5, Array[i].GetColor());
gCanvas.Render();
}
bool myCountIf(CTest const & iVal)
{
return iVal == CTest(200);
}
bool myCountIf2(CTest const & iVal)
{
return !(iVal < CTest(300));
}
//class CTest -
#pragma once
#include "Drawer.h"
class CTest
{
int _iHeight;
COLORREF _Color;
public:
CTest(int iH = 0, COLORREF Col = RGB(rand() % 255, rand() % 255, rand() % 255)):_iHeight(iH), _Color(Col) {}
//accessors
int GetY()const {return _iHeight;}
COLORREF GetColor()const {return _Color;}
//mutators
void Set()
{
_Color = RGB(255, 0, 0);
}
//operators
bool operator<(CTest const & RHS) const
{
return _iHeight < RHS._iHeight;
}
bool operator==(CTest const & RHS)const
{
return _iHeight == RHS._iHeight;
}
~CTest(void);
};
The problem is in the part with all the *'s.
I'm pretty new to the STL (other than basic sorting and vectors), so any help would be appreciated.