[C++] Using vector for simulation
Design a data structure that mocks the behaviour of a web browser.\n\n# Explanation\n\nThe key is to understand the behaviour of a web browser. It acts like a s
Design a data structure that mocks the behaviour of a web browser.\n\n# Explanation\n\nThe key is to understand the behaviour of a web browser. It acts like a stack with a pointer. When you go "forward" / "backward", you move the pointer closer / further from the top of the stack; and when you visit a new webpage, you pop all the pages on top of the pointer, insert in the new page and point to it.\n\nThe time complexity of each operation:\n\n* `visit`: `O(n)` (`n` is the size of stack)\n* `back`: `O(1)`\n* `forward`: `O(1)`\n\n# C++ Solution\n\n```cpp\nclass BrowserHistory {\n    vector<string> history;\n    int pos;\n    \npublic:\n    BrowserHistory(string homepage) {\n        history.push_back(homepage);\n        pos = 0;\n    }\n    \n    void visit(string url) {\n        while (pos < history.size() - 1) {\n            history.pop_back();\n        }\n        history.push_back(url);\n        pos++;\n    }\n    \n    string back(int steps) {\n        pos -= steps;\n        if (pos < 0) pos = 0;\n        return history[pos];\n    }\n    \n    string forward(int steps) {\n        pos += steps;\n        if (pos >= history.size()) pos = history.size() - 1;\n        return history[pos];\n    }\n};\n```
[]