{"pageProps":{"code":{"CMakeLists.txt":{"name":"CMakeLists.txt","content":"add_executable(erase-invalidate erase-invalidate.cpp)\n\nadd_executable(iterator-invalidate iterator-invalidate.cpp)\n\nadd_executable(vector-invalidate vector-invalidate.cpp)\n\nadd_executable(intstack intstack.cpp)\n\n","path":"code/6771/24T2/4.2/CMakeLists.txt","fileext":"txt"},"auto-reverse.cpp":{"name":"auto-reverse.cpp","content":"class Container {\n    // Make the iterator using these.\n    using reverse_iterator = std::reverse_iterator<iterator>;\n    using const_reverse_iterator = std::reverse_iterator<const_iterator>;\n\n    // Need to define these.\n    reverse_iterator rbegin() { return reverse_iterator { end() }; }\n    reverse_iterator rend() { return reverse_iterator { begin() }; }\n\n    // If you want const reverse iterators (hint: you do), define these.\n    const_reverse_iterator rbegin() const { return crbegin(); }\n    const_reverse_iterator rend() const { return crend(); }\n    const_reverse_iterator crbegin() const { return const_reverse_iterator { cend() }; }\n    const_reverse_iterator crend() const { return const_reverse_iterator { cbegin() }; }\n};","path":"code/6771/24T2/4.2/auto-reverse.cpp","fileext":"cpp"},"container-requirements.cpp":{"name":"container-requirements.cpp","content":"class Container {\n    // Make the iterator using one of these by convention.\n    class iterator {\n        ...\n    };\n    using iterator = ...;\n\n    // Need to define these.\n    iterator begin();\n    iterator end();\n\n    // If you want const iterators (hint: you do), define these.\n    const_iterator begin() const { return cbegin(); }\n    const_iterator cbegin() const;\n    const_iterator end() const { return cend(); }\n    const_iterator cend() const;\n};","path":"code/6771/24T2/4.2/container-requirements.cpp","fileext":"cpp"},"custom-bidirectional.cpp":{"name":"custom-bidirectional.cpp","content":"class Container {\n    // Make the iterator\n    class reverse_iterator {\n        ...\n    };\n    // or\n    using reverse_iterator = ...;\n\n    // Need to define these.\n    reverse_iterator rbegin();\n    reverse_iterator rend();\n\n    // If you want const reverse iterators (hint: you do), define these.\n    const_reverse_iterator rbegin() const { return crbegin(); }\n    const_reverse_iterator crbegin();\n    const_reverse_iterator rend() const { return crend(); }\n    const_reverse_iterator crend() const;\n};","path":"code/6771/24T2/4.2/custom-bidirectional.cpp","fileext":"cpp"},"custom-iter-class.cpp":{"name":"custom-iter-class.cpp","content":"class Iterator {\npublic:\n    using iterator_category = std::forward_iterator_tag;\n    using value_type = T;\n    using reference = T&;\n    using pointer = T*; // Not strictly required, but nice to have.\n    using difference_type = int;\n\n    reference operator*() const;\n    Iterator& operator++();\n    Iterator operator++(int)\n    {\n        auto copy { *this };\n        ++(*this);\n        return copy;\n    }\n    // This one isn't strictly required, but it's nice to have.\n    pointer operator->() const { return &(operator*()); }\n\n    friend bool operator==(const Iterator& lhs, const Iterator& rhs) {... };\n    friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return !(lhs == rhs); }\n};","path":"code/6771/24T2/4.2/custom-iter-class.cpp","fileext":"cpp"},"erase-invalidate.cpp":{"name":"erase-invalidate.cpp","content":"#include <vector>\n\nint main()\n{\n    std::vector v { 1, 2, 3, 4, 5 };\n    // Erase all even numbers (C++11 and later)\n    for (auto it = v.begin(); it != v.end();) {\n        if (*it % 2 == 0) {\n            it = v.erase(it);\n        } else {\n            ++it;\n        }\n    }\n}","path":"code/6771/24T2/4.2/erase-invalidate.cpp","fileext":"cpp"},"intstack.cpp":{"name":"intstack.cpp","content":"#include <algorithm>\n#include <iostream>\n#include <iterator>\n#include <utility>\n\n#include \"./intstack.h\"\n\nint main()\n{\n    IntStack l;\n    l.push(5);\n    l.push(4);\n    l.push(3);\n    l.push(2);\n    l.push(1);\n\n    std::cout << std::boolalpha;\n    std::cout << \"Head is \" << l.top() << '\\n';\n    l.pop();\n    std::cout << \"After popping, head is \" << l.top() << '\\n';\n\n    std::cout << \"Algorithms\\n\";\n    std::copy(begin(l), end(l), std::ostream_iterator<int> { std::cout, \"\\n\" });\n\n    auto check_present = [&](int value) {\n        std::cout << \"Is \" << value << \" present: \" << (std::find(begin(l), end(l), 5) != end(l))\n                  << '\\n';\n    };\n    check_present(5);\n    check_present(99);\n    (*begin(l)) = 99;\n    check_present(99);\n\n    // TODO(lecture): This should fail to compile\n    (*cbegin(l)) = 100;\n\n    std::cout << \"Range-for\\n\";\n    for (const auto& item : l) {\n        std::cout << item << '\\n';\n    }\n}\n","path":"code/6771/24T2/4.2/intstack.cpp","fileext":"cpp"},"intstack.h":{"name":"intstack.h","content":"#include <algorithm>\n#include <iostream>\n#include <iterator>\n#include <memory>\n#include <utility>\n\nusing std::begin;\nusing std::cbegin;\nusing std::end;\n\nclass IntStack {\nprivate:\n    struct Node {\n        Node(int value, std::unique_ptr<Node>&& next)\n            : value { value }\n            , next { std::move(next) }\n        {\n        }\n        int value;\n        std::unique_ptr<Node> next;\n    };\n\n    // Exercise to the reader once we've covered templates:\n    // Try making the const iterator and the non-const iterator with one class template.\n    class Iterator {\n    public:\n        using iterator_category = std::forward_iterator_tag;\n        using value_type = int;\n        using reference = int&;\n        using pointer = int*;\n        using difference_type = int;\n\n        reference operator*() const\n        {\n            return node_->value;\n        }\n        pointer operator->() const\n        {\n            return &(operator*());\n        }\n        Iterator operator++()\n        {\n            node_ = node_->next.get();\n            return *this;\n        }\n        Iterator operator++(int)\n        {\n            auto copy { *this };\n            ++(*this);\n            return copy;\n        }\n\n        friend bool operator==(const Iterator& lhs, const Iterator& rhs)\n        {\n            return lhs.node_ == rhs.node_;\n        }\n\n        friend bool operator!=(const Iterator& lhs, const Iterator& rhs)\n        {\n            return !(lhs == rhs);\n        }\n\n    private:\n        explicit Iterator(Node* node)\n            : node_ { node }\n        {\n        }\n        Node* node_;\n\n        friend class IntStack;\n    };\n\npublic:\n    // TODO(lecture): show how make const and non-const iterators during lecture.\n    using iterator = Iterator;\n    using const_iterator = Iterator;\n\n    iterator begin()\n    {\n        return iterator { head_.get() };\n    }\n    const_iterator begin() const\n    {\n        return cbegin();\n    }\n    const_iterator cbegin() const\n    {\n        return const_iterator { head_.get() };\n    }\n    iterator end()\n    {\n        return iterator { nullptr };\n    }\n    const_iterator end() const\n    {\n        return cend();\n    }\n    const_iterator cend() const\n    {\n        return const_iterator { nullptr };\n    }\n\n    void push(int value)\n    {\n        head_ = std::make_unique<Node>(value, std::move(head_));\n    }\n\n    // TODO(students): Why doesn't std::stack::pop return the value you pop?\n    void pop()\n    {\n        head_ = std::move(head_->next);\n    }\n\n    const int& top() const\n    {\n        return *cbegin();\n    }\n    int& top()\n    {\n        return *begin();\n    }\n\nprivate:\n    std::unique_ptr<Node> head_;\n};\n","path":"code/6771/24T2/4.2/intstack.h","fileext":"h"},"iterator-invalidate.cpp":{"name":"iterator-invalidate.cpp","content":"#include <vector>\n\nint main()\n{\n    std::vector v { 1, 2, 3, 4, 5 };\n    // Copy all 2s\n    for (auto it = v.begin(); it != v.end(); ++it) {\n        if (*it == 2) {\n            v.push_back(2);\n        }\n    }\n    // Erase all 2s\n    for (auto it = v.begin(); it != v.end(); ++it) {\n        if (*it == 2) {\n            v.erase(it);\n        }\n    }\n}\n","path":"code/6771/24T2/4.2/iterator-invalidate.cpp","fileext":"cpp"},"random-access.cpp":{"name":"random-access.cpp","content":"class Iterator {\n    ... using reference = T&;\n    using difference_type = int;\n\n    Iterator& operator+=(difference_type rhs) {... } Iterator& operator-=(difference_type rhs) { return *this += (-rhs); }\n    reference operator[](difference_type index) { return *(*this + index); }\n\n    friend Iterator operator+(const Iterator& lhs, difference_type rhs)\n    {\n        Iterator copy { *this };\n        return copy += rhs;\n    }\n    friend Iterator operator+(difference_type lhs, const Iterator& rhs) { return rhs + lhs; }\n    friend Iterator operator-(const Iterator& lhs, difference_type rhs) { return lhs + (-rhs); }\n    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs) { ... }\n\n    friend bool operator<(Iterator lhs, Iterator rhs) { return rhs - lhs > 0; }\n    friend bool operator>(Iterator lhs, Iterator rhs) { return rhs - lhs < 0; }\n    friend bool operator<=(Iterator lhs, Iterator rhs) { !(lhs > rhs); }\n    friend bool operator>=(Iterator lhs, Iterator rhs) { !(lhs < rhs); }\n}","path":"code/6771/24T2/4.2/random-access.cpp","fileext":"cpp"},"vector-invalidate.cpp":{"name":"vector-invalidate.cpp","content":"#include <vector>\n\nint main()\n{\n    std::vector v { 1, 2, 3, 4, 5 };\n    // Copy all 2s\n    for (auto it = v.begin(); it != v.end(); ++it) {\n        if (*it == 2) {\n            v.push_back(2);\n        }\n    }\n}","path":"code/6771/24T2/4.2/vector-invalidate.cpp","fileext":"cpp"}}},"__N_SSG":true}