fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. class A {
  5.  
  6. public:
  7. void foo(std::string state, std::string action, double const& reward) {
  8. if (heuristic.count(action)) {
  9. updateCoefficients(state, heuristic[action], reward);
  10. } else {
  11. std::vector<std::vector<double>> coefs(20, std::vector<double>(2, 0.0));
  12. heuristic[action] = coefs;
  13. }
  14. }
  15. private:
  16. virtual void updateCoefficients(std::string /*state*/, std::vector<std::vector<double>>& /*coefs*/,
  17. double /*reward*/) {}
  18. public:
  19. std::map<std::string, std::vector<std::vector<double>>> heuristic;
  20. };
  21.  
  22. class B : public A {
  23. virtual void updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
  24. double reward) override;
  25. };
  26.  
  27. void B::updateCoefficients(std::string state, std::vector<std::vector<double>>& coefs,
  28. double reward) {
  29. for(unsigned i = 0; i < coefs.size(); i++) {
  30. coefs[i][0] += 1;
  31. }
  32.  
  33. for(unsigned i = 0; i < coefs.size(); i++) {
  34. coefs[i][1] += 2;
  35. }
  36. }
  37.  
  38. using namespace std;
  39.  
  40. int main() {
  41.  
  42. A* instance = new B();
  43. instance->foo("aaa", "bbb", 123);
  44. cout << "First call" << endl;
  45.  
  46. for (auto h : instance->heuristic)
  47. for (auto b : h.second)
  48. for (auto c : b)
  49. cout << c << " ";
  50. cout << endl;
  51. instance->foo("aaa", "bbb", 123);
  52. cout << "Updated" << endl;
  53.  
  54. for (auto h : instance->heuristic)
  55. for (auto b : h.second)
  56. for (auto c : b)
  57. cout << c << " ";
  58. cout << endl;
  59. // your code goes here
  60. return 0;
  61. }
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
First call
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Updated
1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2