fork download
  1. #include <vector>
  2. #include <iostream>
  3.  
  4. struct Node
  5. {
  6. Node(int i): i_(i) {}
  7. int i_;
  8. };
  9.  
  10. class FSM{
  11. private:
  12. std::vector<Node> nodeList;
  13. int cap;
  14. int obs;
  15. int topNode;
  16.  
  17. public:
  18. FSM(int nodeCap, int numObs){
  19. cap = nodeCap;
  20. obs = numObs;
  21. topNode = -1;
  22. }
  23.  
  24. bool addNode(){
  25. if (isFull()) return false;
  26. nodeList.push_back(Node(obs));
  27. topNode++;
  28. return true;
  29. }
  30.  
  31. bool isFull() { return obs >= cap ? true : false; }
  32.  
  33. void printNodes()
  34. {
  35. for(Node& n : nodeList) { std::cout << "(" << &n << "," << n.i_ << ")" << std::endl; }
  36. }
  37.  
  38. void setNode(int nodeIdx, int newVal)
  39. {
  40. nodeList.at(nodeIdx).i_ = newVal;
  41. }
  42. };
  43.  
  44. int main()
  45. {
  46. FSM machine(2, 0);
  47. machine.addNode();
  48. machine.addNode();
  49.  
  50. std::cout << "Nodes: " << std::endl;
  51. machine.printNodes();
  52. machine.setNode(1, 3);
  53.  
  54. std::cout << "Nodes: " << std::endl;
  55. machine.printNodes();
  56.  
  57. return 0;
  58. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Nodes: 
(0x82ec018,0)
(0x82ec01c,0)
Nodes: 
(0x82ec018,0)
(0x82ec01c,3)