Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // file test.cpp //
- TEST_CASE("Decrement operator moves the iterator backward", "[stage2]") {
- list l;
- append_to_list(l, { 5.55, 6.66, 7.77, 8.88 });
- auto it = l.end();
- SECTION("Prefix") {
- REQUIRE(*(--it) == 8.88);
- REQUIRE(*(--it) == 7.77);
- REQUIRE(*(--it) == 6.66);
- REQUIRE(*(--it) == 5.55);
- REQUIRE(it == l.begin());
- }
- SECTION("Postfix") {
- it--;
- REQUIRE(*(it--) == 8.88);
- REQUIRE(*(it--) == 7.77);
- REQUIRE(*(it--) == 6.66);
- REQUIRE(*it == 5.55);
- REQUIRE(it == l.begin());
- }
- }
- // list.hpp //
- class list{
- ...
- public:
- ...
- class iterator{
- node* current_ptr = nullptr;
- const list* o_list = nullptr;
- public:
- iterator(node* ptr, const list* gen);
- iterator& operator++();
- iterator operator++(int);
- iterator& operator--();
- iterator operator--(int);
- }
- }
- // file list.cpp // implementation of class iterator //
- list::iterator::iterator(node *ptr, const list *gen) {
- this->current_ptr = ptr;
- this->o_list = gen;
- }
- list::iterator& list::iterator::operator++() {
- current_ptr = this->current_ptr->next;
- return *this;
- }
- list::iterator& list::iterator::operator--() { // ??? Decrement operator moves the iterator backward, Prefix
- current_ptr = this->current_ptr->prev;
- return *this;
- }
- list::iterator list::iterator::operator++(int)
- {
- iterator old(*this);
- ++(*this);
- return old;
- }
- list::iterator list::iterator::operator--(int)
- {
- iterator left(*this);
- --(*this);
- return left;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement