summarize
In C++, a chained table iterator is a tool used to traverse the elements of a chained table (such as std::list). A linked table is a data structure in which each element (node) contains a data value and a pointer to the next node. A chained table iterator allows access to the elements of a chained table in a manner similar to an array, but without directly manipulating the pointer.
The role of chained table iterators
-
Accessing Elements: The Chained Table Iterator enables you to access each element of a chained table sequentially, as if you were traversing the elements in an array.
-
Traversing a Chained Table: With iterators, you can perform traversal operations by moving forward or backward in a chained table. This makes it easy and intuitive to perform various operations (such as finding, modifying, deleting, etc.) in a linked table.
-
Abstract operations: Iterators provide a uniform way to access different types of data structures. Whether it's a linked list, an array, or some other container, iterators are used in roughly the same way, which makes the code more general and easier to maintain.
usage example
#include <iostream>
#include <list>
int main() {
std::list<int> myList = {1, 2, 3, 4, 5};
// Traversing a Chained Table with Iterators
for (std::list<int>::iterator it = (); it != (); ++it) {
std::cout << *it << " "; // Outputting chained table elements
}
return 0;
}
Why should you set up iterators for your own classes?
Refer to the following chained table class
class List {
public.
List(): head(new Node()) { }
~List().
bool push(int x, int y); // Inserts a new coordinate into the head.
bool pop(int x, int y); // Find the specified coordinate and delete it.
private.
Node* head.
};
In this chain table, the definition ofpush
cap (a poem)pop
Two methods, now assuming that we need to be able to call each node of the chain table externally step by step, starting from the first node, there is a simple way to implement this:
- define
search(int i)
function, starting from the beginning and querying backwardsi
nodes - Incrementing node indexes externally using a for loop
i
。
A pseudo-code is given here:
for (int i = 0; i < 10; ++i) {
Node cur = (i);
std::cout << cur << std::endl;
}
The above method enables traversal of the linked table nodes externally, however, when the index is large, we have to use a more efficient method given that the arithmetic overhead is extremely high given that we need to visit the index from the beginning every time.
How do I set up an iterator method for a class?
Observe how iterators are used in the standard library:
for (std::list<int>::iterator it = (); it ! = (); ++it) {
std::cout << *it << " "; // output the chain table elements
}
We learned that the following needs to be implemented:
- Define the iterator class, containing a
Node*
pointer to a typecur
that points to the current element. - In the chained table class, define
begin
、end
function, pointing to the first element and the tail node, respectively. - define
!=
operator to support comparing two pointers to be the same. - define
++
operator, allowing for more portable traversal of nodes. - define
*
operator that makes it possible to access the value of a node using a pointer method.
We can complete the realization in turn
class List {
public:
List() : head(new Node()) { }
~List();
bool push(int x, int y);
bool pop(int x, int y);
// Define the iterator class
class Iterator {
public:
// constructor
Iterator(Node* node) : cur(node) {}
// pointer operator (computing)
Cell& operator*() { return cur->cell; }
// prepended self-incrementing operator
Iterator& operator++() {
if (cur) cur = cur->next;
return *this;
}
// inequality operator
bool operator!=(const Iterator& other) const { return cur != ; }
private:
// curfield
Node* cur;
}
// Definebegin()、end()methodologies
Iterator begin() const { return Iterator(head->next); }
Iterator end() const { return Iterator(nullptr); }
private:
Node* head;
};
After completing the above implementation, we can use the iterator method to access class members quickly.
#include <iostream>
#include ""
int main() {
List ROI;
// insertion node
(0, 0);
(0, 1);
(0, 2);
(0, 3);
for (List::Iterator it = (); it != (); ++it) {
std::cout << *it << "\n";
}
return 0;
}