Create iterators over the elements in the list
#Create iterators over the elements in the list Here'sHere's how to go about creating iterators for this collection. They will naturally be forward iterators, as we can't traverse the singly-linked list backwards.
First, let's declare the iterator class, and begin()
and end()
methods to create them:
public:
class iterator;
iterator begin();
iterator end();
The state within an iterator is obviously its current Node
:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node;
}
For a forward iterator, we need to be able to dereference and to increment it; also we need to be able to compare iterators for equality, and to construct (including default-construct) instances:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node = nullptr;
public:
iterator(Node *node = nullptr)
: node(node)
{}
bool operator!=(const iterator& other) const { return node != other.node; }
bool operator==(const iterator& other) const { return node == other.node; }
T& operator*() const { return node->data; }
T& operator->() const { return node->data; }
iterator& operator++() { node = node->next.get(); return *this; }
};
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::begin()
{
return head.get();
}
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::end()
{
return {};
}
To use with standard algorithms, we need to provide some type names that can be found by std::iterator_traits
:
#include <iterator>
template<class T>
class SingleLinkedList<T>::iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
Then we can use iterators to simplify our code:
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
return std::find(begin(), end(), x) != end();
}
Exercise
##Exercise
ImplementImplement a const
iterator for the list. You'll start with these methods in SingleLinkedList
:
class const_iterator;
const_iterator cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
and the iterator traits will refer to const T
in place of T
.
#Create iterators over the elements in the list Here's how to go about creating iterators for this collection. They will naturally be forward iterators, as we can't traverse the singly-linked list backwards.
First, let's declare the iterator class, and begin()
and end()
methods to create them:
public:
class iterator;
iterator begin();
iterator end();
The state within an iterator is obviously its current Node
:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node;
}
For a forward iterator, we need to be able to dereference and to increment it; also we need to be able to compare iterators for equality, and to construct (including default-construct) instances:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node = nullptr;
public:
iterator(Node *node = nullptr)
: node(node)
{}
bool operator!=(const iterator& other) const { return node != other.node; }
bool operator==(const iterator& other) const { return node == other.node; }
T& operator*() const { return node->data; }
T& operator->() const { return node->data; }
iterator& operator++() { node = node->next.get(); return *this; }
};
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::begin()
{
return head.get();
}
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::end()
{
return {};
}
To use with standard algorithms, we need to provide some type names that can be found by std::iterator_traits
:
#include <iterator>
template<class T>
class SingleLinkedList<T>::iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
Then we can use iterators to simplify our code:
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
return std::find(begin(), end(), x) != end();
}
##Exercise
Implement a const
iterator for the list. You'll start with these methods in SingleLinkedList
:
class const_iterator;
const_iterator cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
and the iterator traits will refer to const T
in place of T
.
Create iterators over the elements in the list
Here's how to go about creating iterators for this collection. They will naturally be forward iterators, as we can't traverse the singly-linked list backwards.
First, let's declare the iterator class, and begin()
and end()
methods to create them:
public:
class iterator;
iterator begin();
iterator end();
The state within an iterator is obviously its current Node
:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node;
}
For a forward iterator, we need to be able to dereference and to increment it; also we need to be able to compare iterators for equality, and to construct (including default-construct) instances:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node = nullptr;
public:
iterator(Node *node = nullptr)
: node(node)
{}
bool operator!=(const iterator& other) const { return node != other.node; }
bool operator==(const iterator& other) const { return node == other.node; }
T& operator*() const { return node->data; }
T& operator->() const { return node->data; }
iterator& operator++() { node = node->next.get(); return *this; }
};
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::begin()
{
return head.get();
}
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::end()
{
return {};
}
To use with standard algorithms, we need to provide some type names that can be found by std::iterator_traits
:
#include <iterator>
template<class T>
class SingleLinkedList<T>::iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
Then we can use iterators to simplify our code:
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
return std::find(begin(), end(), x) != end();
}
Exercise
Implement a const
iterator for the list. You'll start with these methods in SingleLinkedList
:
class const_iterator;
const_iterator cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
and the iterator traits will refer to const T
in place of T
.
#Create iterators over the elements in the list Here's how to go about creating iterators for this collection. They will naturally be forward iterators, as we can't traverse the singly-linked list backwards.
First, let's declare the iterator class, and begin()
and end()
methods to create them:
public:
class iterator;
iterator begin();
iterator end();
The state within an iterator is obviously its current Node
:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node;
}
For a forward iterator, we need to be able to dereference and to increment it; also we need to be able to compare iterators for equality, and to construct (including default-construct) instances:
template<class T>
class SingleLinkedList<T>::iterator
{
Node* node = nullptr;
public:
iterator(Node *node = nullptr)
: node(node)
{}
bool operator!=(const iterator& other) const { return node != other.node; }
bool operator==(const iterator& other) const { return node == other.node; }
T& operator*() const { return node->data; }
T& operator->() const { return node->data; }
iterator& operator++() { node = node->next.get(); return *this; }
};
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::begin()
{
return head.get();
}
template<class T>
typename SingleLinkedList<T>::iterator SingleLinkedList<T>::end()
{
return {};
}
To use with standard algorithms, we need to provide some type names that can be found by std::iterator_traits
:
#include <iterator>
template<class T>
class SingleLinkedList<T>::iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = T*;
using reference = T&;
Then we can use iterators to simplify our code:
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
return std::find(begin(), end(), x) != end();
}
##Exercise
Implement a const
iterator for the list. You'll start with these methods in SingleLinkedList
:
class const_iterator;
const_iterator cbegin() const;
const_iterator cend() const;
const_iterator begin() const;
const_iterator end() const;
and the iterator traits will refer to const T
in place of T
.