STL - Algorithm - find_if

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

STL - Algorithm - find_if

 
0
  #1
Nov 17th, 2008
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:

  1. #include "Test.h"
  2. #include "Drawer.h"
  3. #include <iostream>
  4. using namespace std;
  5. #include <Algorithm>
  6.  
  7. CDrawer gCanvas(RGB(0,0,0));
  8. void Draw(CTest [], int);
  9. bool myCountIf(CTest const &);
  10. bool myCountIf2(CTest const &);
  11. // ICA 19 ** Fill in the required blocks, labels and pauses are supplied
  12. int main( void )
  13. {
  14. CTest a[150];
  15. CTest b[150];
  16. CTest c[150];
  17.  
  18. for (int i(0); i < 150; ++i)
  19. {
  20. a[i] = CTest(300);
  21. b[i]= CTest((i + 1) * 4);
  22. c[i] =CTest(rand() % 600);
  23. }
  24.  
  25. cout << "a - Fixed 300\n";
  26. Draw( a, 150 );
  27. cin.get();
  28. cout << "b - Increment\n";
  29. Draw( b, 150 );
  30. cin.get();
  31. cout << "c - Random\n";
  32. Draw( c, 150 );
  33. cin.get();
  34.  
  35. // Min and Max of c here
  36. cout << "Min of c : " << min_element(c, c + 150)->GetY() << endl;
  37. cout << "Max of c : " << max_element(c, c + 150)->GetY() << endl;
  38. cin.get();
  39.  
  40. // Count of 200, Count > 300 here
  41. cout << "Count of b == 200 : " << count_if(b, b + 150, myCountIf) << endl;
  42. cout << "Count of b > 300 : " << count_if(b, b + 150, myCountIf2) << endl;
  43. cin.get();
  44.  
  45. // Find and Set() 200 in b
  46. cout << "Found 200 in b, setting to RED\n";
  47. find_if(b, b + 150, myCountIf)->Set();
  48. Draw( b, 150 );
  49. cin.get();
  50.  
  51. // Find and Set() all > 300 in c here ***************************
  52. //for_each doesnt seem to work here
  53. cout << "Found > 300 in c, setting to RED\n";
  54. for_each(c, b + 150, find_if(b, b + 150, myCountIf)->Set());
  55. Draw( c, 150 );
  56. cin.get();
  57. return 0;
  58. }
  59.  
  60. void Draw(CTest Array[], int iCount)
  61. {
  62. gCanvas.Clear();
  63. for (int i(0); i < iCount; ++i)
  64. gCanvas.AddLine(i * 5, 600, i * 5, 600 - Array[i].GetY(),5, Array[i].GetColor());
  65. gCanvas.Render();
  66. }
  67.  
  68. bool myCountIf(CTest const & iVal)
  69. {
  70. return iVal == CTest(200);
  71. }
  72. bool myCountIf2(CTest const & iVal)
  73. {
  74. return !(iVal < CTest(300));
  75. }
  76.  
  77. //class CTest -
  78. #pragma once
  79. #include "Drawer.h"
  80.  
  81.  
  82. class CTest
  83. {
  84. int _iHeight;
  85. COLORREF _Color;
  86. public:
  87. CTest(int iH = 0, COLORREF Col = RGB(rand() % 255, rand() % 255, rand() % 255)):_iHeight(iH), _Color(Col) {}
  88. //accessors
  89. int GetY()const {return _iHeight;}
  90. COLORREF GetColor()const {return _Color;}
  91. //mutators
  92. void Set()
  93. {
  94. _Color = RGB(255, 0, 0);
  95. }
  96. //operators
  97. bool operator<(CTest const & RHS) const
  98. {
  99. return _iHeight < RHS._iHeight;
  100. }
  101. bool operator==(CTest const & RHS)const
  102. {
  103. return _iHeight == RHS._iHeight;
  104. }
  105.  
  106. ~CTest(void);
  107. };
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.
Last edited by skatamatic; Nov 17th, 2008 at 9:42 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: STL - Algorithm - find_if

 
2
  #2
Nov 17th, 2008
find_if returns an iterator to the matched item, and for_each is retarded.
  1. CTest *it = b;
  2. CTest *end = b + 150;
  3.  
  4. while ( ( it = find_if ( it, end, myCountIf ) ) != end ) {
  5. it->Set();
  6. ++it;
  7. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 390
Reputation: skatamatic will become famous soon enough skatamatic will become famous soon enough 
Solved Threads: 39
skatamatic skatamatic is offline Offline
Posting Whiz

Re: STL - Algorithm - find_if

 
0
  #3
Nov 17th, 2008
Originally Posted by Narue View Post
find_if returns an iterator to the matched item, and for_each is retarded.
  1. CTest *it = b;
  2. CTest *end = b + 150;
  3.  
  4. while ( ( it = find_if ( it, end, myCountIf ) ) != end ) {
  5. it->Set();
  6. ++it;
  7. }
Lol. Thanks, I already solved it with a similar loop. I was thinking that there was a single function to do the whole loop for me .

Here's what i did:

  1. CTest * lp = c - 1; //last found pointer (start at 1 before c)
  2. //this loop is a bit confusing...
  3. while (lp < c + 150)
  4. (lp = find_if(++lp, c + 150, myCountIf2))->Set();
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1031 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC