Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Iterator pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Example == {{main|Iterator}} Some languages standardize syntax. C++ and Python are notable examples. <!-- READ NOTE BELOW BEFORE ADDING EXAMPLES Wikipedia is not a list of examples. Do not add examples from your favorite programming language here; this page exists to explain the design pattern, not to show how it interacts with subtleties of every language extant. Feel free to add examples here: http://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Iterator --> === C++ === [[C++]] implements iterators with the semantics of [[pointer (computer programming)|pointer]]s in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as <code>std::sort</code> can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an "end" iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator [[concept (generic programming)|concept]]. This C++11 implementation is based on chapter "Generalizing vector yet again".<ref>{{cite book |author=Bjarne Stroustrup |title=Programming: Principles and Practice using C++ |edition=2 |publisher=Addison Wesley |year=2014 |isbn=978-0-321-99278-9 |pages=729 ff.}}</ref> <syntaxhighlight lang="c++"> #include <iostream> #include <stdexcept> #include <initializer_list> class Vector { public: using iterator = double*; iterator begin() { return elem; } iterator end() { return elem + sz; } Vector(std::initializer_list<double> lst) :elem(nullptr), sz(0) { sz = lst.size(); elem = new double[sz]; double* p = elem; for (auto i = lst.begin(); i != lst.end(); ++i, ++p) { *p = *i; } } ~Vector() { delete[] elem; } int size() const { return sz; } double& operator[](int n) { if (n < 0 || n >= sz) throw std::out_of_range("Vector::operator[]"); return elem[n]; } Vector(const Vector&) = delete; // rule of three Vector& operator=(const Vector&) = delete; private: double* elem; int sz; }; int main() { Vector v = {1.1*1.1, 2.2*2.2}; for (const auto& x : v) { std::cout << x << '\n'; } for (auto i = v.begin(); i != v.end(); ++i) { std::cout << *i << '\n'; } for (auto i = 0; i <= v.size(); ++i) { std::cout << v[i] << '\n'; } } </syntaxhighlight> The program output is <syntaxhighlight lang="c++"> 1.21 4.84 1.21 4.84 1.21 4.84 terminate called after throwing an instance of 'std::out_of_range' what(): Vector::operator[] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)