fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. class Module
  7. {
  8. private:
  9. std::string code;
  10. std::string name ;
  11. std::string lect ;
  12. int cwWeight ;
  13. double exMark = 0.0; //initialised as 0
  14. public:
  15. Module(const std::string& id, const std::string& title,
  16. const std::string& lecturer, const int courseworkWeight)
  17. :code(id), name(title),
  18. lect(lecturer), cwWeight(courseworkWeight)
  19. {
  20. // other things
  21. }
  22. ~Module(){ std::cout << code << " Deleted..\n"; }
  23. // getters for id
  24. std::string getId()const { return code; }
  25. // setter for exMark
  26. void setExMark(const double newMark) { exMark = newMark; }
  27. void print()const
  28. {
  29. std::cout << code << " " << name << " " << lect << " "
  30. << " exMark: " << exMark << std::endl;
  31. }
  32. };
  33.  
  34.  
  35. int main()
  36. {
  37. std::vector<Module*> moduleVector
  38. {
  39. new Module("one", "title 1", "lecture 1", 1),
  40. new Module("two", "title 2", "lecture 2", 2),
  41. new Module("three", "title 3", "lecture 3", 3)
  42. };
  43. // id from file which has to be compared
  44. std::string idFromFile = "two";
  45. // condition
  46. auto Condition = [&idFromFile](Module* element){ return element->getId() == idFromFile; };
  47. auto iter = std::find_if(moduleVector.begin(), moduleVector.end(), Condition);
  48. // if there is a match
  49. if(iter != moduleVector.end())
  50. (*iter)->setExMark(10.0); // set the exMark
  51. // ^^^^^^^^^
  52. // to print
  53. for(const Module* it: moduleVector)
  54. it->print();
  55.  
  56. for( Module* it: moduleVector) // delete
  57. {
  58. delete it;
  59. }
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
one title 1 lecture 1  exMark: 0
two title 2 lecture 2  exMark: 10
three title 3 lecture 3  exMark: 0
one Deleted..
two Deleted..
three Deleted..