| | |
STL - Algorithm - find_if
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
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:
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.
C++ Syntax (Toggle Plain Text)
#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); };
I'm pretty new to the STL (other than basic sorting and vectors), so any help would be appreciated.
Last edited by skatamatic; Nov 17th, 2008 at 9:42 pm.
find_if returns an iterator to the matched item, and for_each is retarded. 

C++ Syntax (Toggle Plain Text)
CTest *it = b; CTest *end = b + 150; while ( ( it = find_if ( it, end, myCountIf ) ) != end ) { it->Set(); ++it; }
New members chased away this month: 4
•
•
Join Date: Nov 2007
Posts: 390
Reputation:
Solved Threads: 39
•
•
•
•
find_if returns an iterator to the matched item, and for_each is retarded.
C++ Syntax (Toggle Plain Text)
CTest *it = b; CTest *end = b + 150; while ( ( it = find_if ( it, end, myCountIf ) ) != end ) { it->Set(); ++it; }
.Here's what i did:
C++ Syntax (Toggle Plain Text)
CTest * lp = c - 1; //last found pointer (start at 1 before c) //this loop is a bit confusing... while (lp < c + 150) (lp = find_if(++lp, c + 150, myCountIf2))->Set();
![]() |
Similar Threads
- STL Help .. Please ... An intresting problem !!! (C++)
- How to search for an element in a 2D vector? (C++)
- STL find_if (C)
Other Threads in the C++ Forum
- Previous Thread: Searching binary files
- Next Thread: More STL Algorithm Trouble
Views: 1031 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






