{"id":1965,"date":"2024-06-22T09:53:00","date_gmt":"2024-06-22T09:53:00","guid":{"rendered":"https:\/\/www.w3computing.com\/articles\/?p=1965"},"modified":"2024-06-22T09:53:05","modified_gmt":"2024-06-22T09:53:05","slug":"how-to-implement-custom-iterators-in-cpp","status":"publish","type":"post","link":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/","title":{"rendered":"How to Implement Custom Iterators in C++"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection, such as arrays, vectors, lists, and more. Custom iterators are essential when you create your own data structures and want them to work seamlessly with the standard algorithms provided by the STL. In this tutorial, we&#8217;ll explore how to implement custom iterators in C++.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction to Iterators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Iterators are objects that allow a programmer to traverse through elements of a collection. They are designed to provide a way to access elements without exposing the underlying representation of the collection.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">STL Iterator Example<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;vector&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iostream&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">vector<\/span>&lt;<span class=\"hljs-keyword\">int<\/span>&gt; vec = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">auto<\/span> it = vec.begin(); it != vec.end(); ++it) {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n    }\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">In the above code, <code>vec.begin()<\/code> returns an iterator to the beginning of the vector, and <code>vec.end()<\/code> returns an iterator to one past the last element. The iterator is used to traverse and print each element.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Types of Iterators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The STL defines several types of iterators based on their capabilities:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input Iterator<\/strong>: Read-only access, single pass.<\/li>\n\n\n\n<li><strong>Output Iterator<\/strong>: Write-only access, single pass.<\/li>\n\n\n\n<li><strong>Forward Iterator<\/strong>: Read and write access, multiple passes, can move only forward.<\/li>\n\n\n\n<li><strong>Bidirectional Iterator<\/strong>: Read and write access, multiple passes, can move both forward and backward.<\/li>\n\n\n\n<li><strong>Random Access Iterator<\/strong>: Read and write access, multiple passes, can move freely (supports arithmetic operations like <code>+<\/code>, <code>-<\/code>).<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Requirements for Custom Iterators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To create a custom iterator, you need to follow certain conventions and implement specific functions. These conventions ensure that your iterator will be compatible with STL algorithms and containers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterator Traits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Iterator traits are used to provide information about the iterator. They define types such as <code>value_type<\/code>, <code>difference_type<\/code>, <code>pointer<\/code>, <code>reference<\/code>, and <code>iterator_category<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Basic Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A custom iterator must support the following operations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dereferencing<\/strong>: Access the value pointed to by the iterator using the <code>*<\/code> operator.<\/li>\n\n\n\n<li><strong>Increment<\/strong>: Move to the next element using the <code>++<\/code> operator.<\/li>\n\n\n\n<li><strong>Equality\/Inequality<\/strong>: Compare iterators using <code>==<\/code> and <code>!=<\/code> operators.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. Implementing a Custom Iterator<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s start with a basic custom iterator for a simple container.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Iterator Traits<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Define the necessary traits for the iterator.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iterator&gt;<\/span><\/span>\n\n<span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SimpleIterator<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-keyword\">using<\/span> value_type = T;\n    <span class=\"hljs-keyword\">using<\/span> difference_type = <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">ptrdiff_t<\/span>;\n    <span class=\"hljs-keyword\">using<\/span> pointer = T*;\n    <span class=\"hljs-keyword\">using<\/span> reference = T&amp;;\n    <span class=\"hljs-keyword\">using<\/span> iterator_category = <span class=\"hljs-built_in\">std<\/span>::forward_iterator_tag;\n\n    SimpleIterator(pointer ptr) : m_ptr(ptr) {}\n\n    reference <span class=\"hljs-keyword\">operator<\/span>*() <span class=\"hljs-keyword\">const<\/span> { <span class=\"hljs-keyword\">return<\/span> *m_ptr; }\n    pointer <span class=\"hljs-keyword\">operator<\/span>-&gt;() { <span class=\"hljs-keyword\">return<\/span> m_ptr; }\n\n    <span class=\"hljs-comment\">\/\/ Prefix increment<\/span>\n    SimpleIterator&amp; <span class=\"hljs-keyword\">operator<\/span>++() {\n        m_ptr++;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    <span class=\"hljs-comment\">\/\/ Postfix increment<\/span>\n    SimpleIterator <span class=\"hljs-keyword\">operator<\/span>++(<span class=\"hljs-keyword\">int<\/span>) {\n        SimpleIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        ++(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>==(<span class=\"hljs-keyword\">const<\/span> SimpleIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> SimpleIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr == b.m_ptr;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>!=(<span class=\"hljs-keyword\">const<\/span> SimpleIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> SimpleIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr != b.m_ptr;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    pointer m_ptr;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Basic Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Now, let&#8217;s create a simple container that uses this iterator.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">SimpleContainer<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-keyword\">using<\/span> iterator = SimpleIterator&lt;T&gt;;\n\n    SimpleContainer(<span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">initializer_list<\/span>&lt;T&gt; init) {\n        size = init.size();\n        data = <span class=\"hljs-keyword\">new<\/span> T&#91;size];\n        <span class=\"hljs-built_in\">std<\/span>::copy(init.begin(), init.end(), data);\n    }\n\n    ~SimpleContainer() {\n        <span class=\"hljs-keyword\">delete<\/span>&#91;] data;\n    }\n\n    <span class=\"hljs-function\">iterator <span class=\"hljs-title\">begin<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> iterator(data); }\n    <span class=\"hljs-function\">iterator <span class=\"hljs-title\">end<\/span><span class=\"hljs-params\">()<\/span> <\/span>{ <span class=\"hljs-keyword\">return<\/span> iterator(data + size); }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    T* data;\n    <span class=\"hljs-keyword\">size_t<\/span> size;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Iterator Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In the <code>SimpleIterator<\/code> class, we have defined basic iterator operations such as dereferencing, incrementing, and comparison. The <code>SimpleContainer<\/code> class uses this iterator.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Example: Custom Iterator for a Simple Container<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s put it all together with a complete example.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;algorithm&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    SimpleContainer&lt;<span class=\"hljs-keyword\">int<\/span>&gt; container = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">auto<\/span> it = container.begin(); it != container.end(); ++it) {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n    }\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n\n    <span class=\"hljs-built_in\">std<\/span>::for_each(container.begin(), container.end(), &#91;](<span class=\"hljs-keyword\">int<\/span> &amp;n) { n *= <span class=\"hljs-number\">2<\/span>; });\n\n    <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">auto<\/span> it = container.begin(); it != container.end(); ++it) {\n        <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; *it &lt;&lt; <span class=\"hljs-string\">\" \"<\/span>;\n    }\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">This example demonstrates how to create and use a custom iterator with a simple container.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">6. Advanced Features<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Input and Output Iterators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Input and output iterators have specific requirements. Let&#8217;s implement an input iterator.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">InputIterator<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-keyword\">using<\/span> value_type = T;\n    <span class=\"hljs-keyword\">using<\/span> difference_type = <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">ptrdiff_t<\/span>;\n    <span class=\"hljs-keyword\">using<\/span> pointer = T*;\n    <span class=\"hljs-keyword\">using<\/span> reference = T&amp;;\n    <span class=\"hljs-keyword\">using<\/span> iterator_category = <span class=\"hljs-built_in\">std<\/span>::input_iterator_tag;\n\n    InputIterator(pointer ptr) : m_ptr(ptr) {}\n\n    reference <span class=\"hljs-keyword\">operator<\/span>*() <span class=\"hljs-keyword\">const<\/span> { <span class=\"hljs-keyword\">return<\/span> *m_ptr; }\n    pointer <span class=\"hljs-keyword\">operator<\/span>-&gt;() { <span class=\"hljs-keyword\">return<\/span> m_ptr; }\n\n    InputIterator&amp; <span class=\"hljs-keyword\">operator<\/span>++() {\n        m_ptr++;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    InputIterator <span class=\"hljs-keyword\">operator<\/span>++(<span class=\"hljs-keyword\">int<\/span>) {\n        InputIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        ++(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>==(<span class=\"hljs-keyword\">const<\/span> InputIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> InputIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr == b.m_ptr;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>!=(<span class=\"hljs-keyword\">const<\/span> InputIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> InputIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr != b.m_ptr;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    pointer m_ptr;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Forward, Bidirectional, and Random Access Iterators<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Bidirectional and random access iterators build on top of forward iterators by adding more functionality.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Bidirectional Iterator<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">BidirectionalIterator<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-keyword\">using<\/span> value_type = T;\n    <span class=\"hljs-keyword\">using<\/span> difference_type = <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">ptrdiff_t<\/span>;\n    <span class=\"hljs-keyword\">using<\/span> pointer = T*;\n    <span class=\"hljs-keyword\">using<\/span> reference = T&amp;;\n    <span class=\"hljs-keyword\">using<\/span> iterator_category = <span class=\"hljs-built_in\">std<\/span>::bidirectional_iterator_tag;\n\n    BidirectionalIterator(pointer ptr) : m_ptr(ptr) {}\n\n    reference <span class=\"hljs-keyword\">operator<\/span>*() <span class=\"hljs-keyword\">const<\/span> { <span class=\"hljs-keyword\">return<\/span> *m_ptr; }\n    pointer <span class=\"hljs-keyword\">operator<\/span>-&gt;() { <span class=\"hljs-keyword\">return<\/span> m_ptr; }\n\n    BidirectionalIterator&amp; <span class=\"hljs-keyword\">operator<\/span>++() {\n        m_ptr++;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    BidirectionalIterator <span class=\"hljs-keyword\">operator<\/span>++(<span class=\"hljs-keyword\">int<\/span>) {\n        BidirectionalIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        ++(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    BidirectionalIterator&amp; <span class=\"hljs-keyword\">operator<\/span>--() {\n        m_ptr--;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    BidirectionalIterator <span class=\"hljs-keyword\">operator<\/span>--(<span class=\"hljs-keyword\">int<\/span>) {\n        BidirectionalIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        --(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>==(<span class=\"hljs-keyword\">const<\/span> BidirectionalIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> BidirectionalIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr == b.m_ptr;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>!=(<span class=\"hljs-keyword\">const<\/span> BidirectionalIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> BidirectionalIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr != b.m_ptr;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    pointer m_ptr;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Random Access Iterator<\/h4>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-keyword\">template<\/span> &lt;<span class=\"hljs-keyword\">typename<\/span> T&gt;\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">RandomAccessIterator<\/span> {<\/span>\n<span class=\"hljs-keyword\">public<\/span>:\n    <span class=\"hljs-keyword\">using<\/span> value_type = T;\n    <span class=\"hljs-keyword\">using<\/span> difference_type = <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-keyword\">ptrdiff_t<\/span>;\n    <span class=\"hljs-keyword\">using<\/span> pointer = T*;\n    <span class=\"hljs-keyword\">using<\/span> reference = T&amp;;\n    <span class=\"hljs-keyword\">using<\/span> iterator_category = <span class=\"hljs-built_in\">std<\/span>::random_access_iterator_tag;\n\n    RandomAccessIterator(pointer ptr) : m_ptr(ptr) {}\n\n    reference <span class=\"hljs-keyword\">operator<\/span>*() <span class=\"hljs-keyword\">const<\/span> { <span class=\"hljs-keyword\">return<\/span> *m_ptr; }\n    pointer <span class=\"hljs-keyword\">operator<\/span>-&gt;() { <span class=\"hljs-keyword\">return<\/span> m_ptr; }\n\n    RandomAccessIterator&amp; <span class=\"hljs-keyword\">operator<\/span>++() {\n        m_ptr++;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    RandomAccessIterator <span class=\"hljs-keyword\">operator<\/span>++(<span class=\"hljs-keyword\">int<\/span>) {\n        RandomAccessIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        ++(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    RandomAccessIterator&amp; <span class=\"hljs-keyword\">operator<\/span>--() {\n        m_ptr--;\n        <span class=\"hljs-keyword\">return<\/span> *<span class=\"hljs-keyword\">this<\/span>;\n    }\n\n    RandomAccessIterator <span class=\"hljs-keyword\">operator<\/span>--(<span class=\"hljs-keyword\">int<\/span>) {\n        RandomAccessIterator tmp = *<span class=\"hljs-keyword\">this<\/span>;\n        --(*<span class=\"hljs-keyword\">this<\/span>);\n        <span class=\"hljs-keyword\">return<\/span> tmp;\n    }\n\n    RandomAccessIterator <span class=\"hljs-keyword\">operator<\/span>+(difference_type n) <span class=\"hljs-keyword\">const<\/span> {\n        <span class=\"hljs-keyword\">return<\/span> RandomAccessIterator(m_ptr + n);\n    }\n\n    RandomAccessIterator <span class=\"hljs-keyword\">operator<\/span>-(difference_type n) <span class=\"hljs-keyword\">const<\/span> {\n        <span class=\"hljs-keyword\">return<\/span> RandomAccessIterator(m_ptr - n);\n    }\n\n    difference_type <span class=\"hljs-keyword\">operator<\/span>-(<span class=\"hljs-keyword\">const<\/span> RandomAccessIterator&amp; other\n\n) <span class=\"hljs-keyword\">const<\/span> {\n        <span class=\"hljs-keyword\">return<\/span> m_ptr - other.m_ptr;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>==(<span class=\"hljs-keyword\">const<\/span> RandomAccessIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> RandomAccessIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr == b.m_ptr;\n    }\n\n    <span class=\"hljs-keyword\">friend<\/span> <span class=\"hljs-keyword\">bool<\/span> <span class=\"hljs-keyword\">operator<\/span>!=(<span class=\"hljs-keyword\">const<\/span> RandomAccessIterator&amp; a, <span class=\"hljs-keyword\">const<\/span> RandomAccessIterator&amp; b) {\n        <span class=\"hljs-keyword\">return<\/span> a.m_ptr != b.m_ptr;\n    }\n\n<span class=\"hljs-keyword\">private<\/span>:\n    pointer m_ptr;\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">7. Testing and Debugging Iterators<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Testing iterators involves checking their behavior with STL algorithms and custom loops. Use unit tests to verify that your iterator behaves as expected in various scenarios.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example Test<\/h3>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"C++\" data-shcb-language-slug=\"cpp\"><span><code class=\"hljs language-cpp\"><span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;cassert&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;algorithm&gt;<\/span><\/span>\n<span class=\"hljs-meta\">#<span class=\"hljs-meta-keyword\">include<\/span> <span class=\"hljs-meta-string\">&lt;vector&gt;<\/span><\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">test_simple_iterator<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    SimpleContainer&lt;<span class=\"hljs-keyword\">int<\/span>&gt; container = {<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>};\n    <span class=\"hljs-keyword\">auto<\/span> it = container.begin();\n    assert(*it == <span class=\"hljs-number\">1<\/span>);\n    ++it;\n    assert(*it == <span class=\"hljs-number\">2<\/span>);\n    it++;\n    assert(*it == <span class=\"hljs-number\">3<\/span>);\n    <span class=\"hljs-built_in\">std<\/span>::for_each(container.begin(), container.end(), &#91;](<span class=\"hljs-keyword\">int<\/span> &amp;n) { n *= <span class=\"hljs-number\">2<\/span>; });\n    assert(*(container.begin()) == <span class=\"hljs-number\">2<\/span>);\n    assert(*(++container.begin()) == <span class=\"hljs-number\">4<\/span>);\n}\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">int<\/span> <span class=\"hljs-title\">main<\/span><span class=\"hljs-params\">()<\/span> <\/span>{\n    test_simple_iterator();\n    <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">cout<\/span> &lt;&lt; <span class=\"hljs-string\">\"All tests passed!\"<\/span> &lt;&lt; <span class=\"hljs-built_in\">std<\/span>::<span class=\"hljs-built_in\">endl<\/span>;\n    <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">0<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">C++<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">cpp<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">8. Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Follow STL Conventions<\/strong>: Ensure your iterators follow the STL conventions for compatibility with standard algorithms.<\/li>\n\n\n\n<li><strong>Iterator Traits<\/strong>: Define the necessary iterator traits for your custom iterator.<\/li>\n\n\n\n<li><strong>Increment and Decrement<\/strong>: Implement both prefix and postfix increment and decrement operators where applicable.<\/li>\n\n\n\n<li><strong>Equality and Inequality<\/strong>: Implement <code>operator==<\/code> and <code>operator!=<\/code> to compare iterators.<\/li>\n\n\n\n<li><strong>Testing<\/strong>: Thoroughly test your iterators with various STL algorithms and custom tests to ensure correctness.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">9. Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Custom iterators are powerful tools for creating seamless and efficient traversal mechanisms for your custom data structures. By adhering to the conventions and implementing the necessary operations, you can create iterators that integrate smoothly with the C++ Standard Library. This tutorial covered the basics of creating custom iterators, from defining iterator traits to implementing various types of iterators. With practice and careful design, you can leverage custom iterators to enhance the usability and performance of your custom containers in C++.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection, such as arrays, vectors, lists, and more. Custom iterators are essential when you create your own data structures and want them to work seamlessly with the standard algorithms provided by the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[9,4],"tags":[],"class_list":["post-1965","post","type-post","status-publish","format-standard","category-cplusplus","category-programming-languages","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Implement Custom Iterators in C++<\/title>\n<meta name=\"description\" content=\"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Implement Custom Iterators in C++\" \/>\n<meta property=\"og:description\" content=\"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-06-22T09:53:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-22T09:53:05+00:00\" \/>\n<meta name=\"author\" content=\"w3compadmin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"w3compadmin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"TechArticle\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/\"},\"author\":{\"name\":\"w3compadmin\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"headline\":\"How to Implement Custom Iterators in C++\",\"datePublished\":\"2024-06-22T09:53:00+00:00\",\"dateModified\":\"2024-06-22T09:53:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/\"},\"wordCount\":619,\"articleSection\":[\"C++\",\"Programming Languages\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/\",\"name\":\"How to Implement Custom Iterators in C++\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\"},\"datePublished\":\"2024-06-22T09:53:00+00:00\",\"dateModified\":\"2024-06-22T09:53:05+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\"},\"description\":\"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/how-to-implement-custom-iterators-in-cpp\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Articles Home\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Programming Languages\",\"item\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/programming-languages\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Implement Custom Iterators in C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#website\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/\",\"name\":\"Developer Articles Hub\",\"description\":\"\",\"alternateName\":\"Developer Articles\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/#\\\/schema\\\/person\\\/a550b3e20d78bb4f79b7c6b7b53f0561\",\"name\":\"w3compadmin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"url\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"contentUrl\":\"https:\\\/\\\/www.w3computing.com\\\/articles\\\/wp-content\\\/litespeed\\\/avatar\\\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266\",\"caption\":\"w3compadmin\"},\"sameAs\":[\"http:\\\/\\\/w3computing.com\\\/articles\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Implement Custom Iterators in C++","description":"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/","og_locale":"en_US","og_type":"article","og_title":"How to Implement Custom Iterators in C++","og_description":"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection","og_url":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/","article_published_time":"2024-06-22T09:53:00+00:00","article_modified_time":"2024-06-22T09:53:05+00:00","author":"w3compadmin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"w3compadmin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"TechArticle","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/#article","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/"},"author":{"name":"w3compadmin","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"headline":"How to Implement Custom Iterators in C++","datePublished":"2024-06-22T09:53:00+00:00","dateModified":"2024-06-22T09:53:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/"},"wordCount":619,"articleSection":["C++","Programming Languages"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/","url":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/","name":"How to Implement Custom Iterators in C++","isPartOf":{"@id":"https:\/\/www.w3computing.com\/articles\/#website"},"datePublished":"2024-06-22T09:53:00+00:00","dateModified":"2024-06-22T09:53:05+00:00","author":{"@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561"},"description":"Iterators in C++ are a fundamental part of the Standard Template Library (STL). They provide a uniform way to access elements of a collection","breadcrumb":{"@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.w3computing.com\/articles\/how-to-implement-custom-iterators-in-cpp\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Articles Home","item":"https:\/\/www.w3computing.com\/articles\/"},{"@type":"ListItem","position":2,"name":"Programming Languages","item":"https:\/\/www.w3computing.com\/articles\/programming-languages\/"},{"@type":"ListItem","position":3,"name":"How to Implement Custom Iterators in C++"}]},{"@type":"WebSite","@id":"https:\/\/www.w3computing.com\/articles\/#website","url":"https:\/\/www.w3computing.com\/articles\/","name":"Developer Articles Hub","description":"","alternateName":"Developer Articles","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.w3computing.com\/articles\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.w3computing.com\/articles\/#\/schema\/person\/a550b3e20d78bb4f79b7c6b7b53f0561","name":"w3compadmin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","url":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","contentUrl":"https:\/\/www.w3computing.com\/articles\/wp-content\/litespeed\/avatar\/bd481d404e42caa2763662a3bfe825f8.jpg?ver=1780141266","caption":"w3compadmin"},"sameAs":["http:\/\/w3computing.com\/articles"]}]}},"featured_image_src":null,"featured_image_src_square":null,"author_info":{"display_name":"w3compadmin","author_link":"https:\/\/www.w3computing.com\/articles\/author\/w3compadmin\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1965","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/comments?post=1965"}],"version-history":[{"count":2,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1965\/revisions"}],"predecessor-version":[{"id":1967,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/posts\/1965\/revisions\/1967"}],"wp:attachment":[{"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/media?parent=1965"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/categories?post=1965"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.w3computing.com\/articles\/wp-json\/wp\/v2\/tags?post=1965"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}