{"id":909,"date":"2010-04-27T20:42:06","date_gmt":"2010-04-27T19:42:06","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=909"},"modified":"2010-04-27T20:57:00","modified_gmt":"2010-04-27T19:57:00","slug":"iterator-wrapping-around-a-class","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/27\/iterator-wrapping-around-a-class\/","title":{"rendered":"iterator &#8211; wrapping around a class"},"content":{"rendered":"<p>An <a href=\"http:\/\/en.wikipedia.org\/wiki\/Iterator\">iterator<\/a> is when you implement a abstract way of cycling through data.  I have below created a internal iterator wrapper that will allow the class to implement a iterator for that class.  Below the class is kinder like a stack where you place one item at the root of the stack and then place another item in a link (next item) attached to the last node of the stack (where the last item is, be it at the root if empty)<\/p>\n<p>You can create a wrapper for the class externally as well, but in this instance I am creating it internally.<\/p>\n<p>To start with I have created a internal class within the stack class called a Node, the Node is the item on the stack (the data inserted in a array as such).  Also I created a basic class definition at the top the class so that the rest of the class knows that there is a thing called a Node, but the implementation is below.<\/p>\n<pre lang=\"cpp\">\r\n    \/\/ forward declaration for the class structure below.\r\n    class Node;\r\n<\/pre>\n<p>As said before, there is a root node that, this is where the start of the array in essence is, and the last node, where the last inserted node as placed.  On each node there is a method to return the value attached to that node and also a link to the next node on the linked list.<\/p>\n<pre lang=\"cpp\">\r\n    class Node {\r\n    public:\r\n\tNode(const int& val) : nextNode(NULL), nodeValue(val) {}\r\n\tint& getVal() {return(nodeValue);}\r\n\tNode* nextNode;\r\n    private:\r\n\tint nodeValue;\r\n    };\r\n<\/pre>\n<p>now we have the node class setup, which means that when we add to the stack we can now place the values added to a dynamic linked list and a way to go through them.  Now we just need to add values into the stacked list.<\/p>\n<pre lang=\"cpp\">\r\nvoid theStack::addValue(int valueToAdd)\r\n{\r\n    if (rootNode == NULL)\r\n    {\r\n\trootNode = new Node(valueToAdd);\r\n\tlastNode = rootNode;\r\n    }else\r\n    {\r\n\tNode* p = new Node(valueToAdd);\r\n\tlastNode->nextNode = p;\r\n\tlastNode =p;\r\n    }\r\n}\r\n<\/pre>\n<p>this will place the new item either at the root node or attached to the next node to the last added node and then re-sets the last node added to point to where the new node, so that when another node is added it will be added in the correct place.<\/p>\n<p><strong>Here comes the iterator part.<\/strong><\/p>\n<p>To start with the internal part of the class, the internal iterator wrapper as such, we use the iterator class to extend\/implement the iterator functions that we have to implement for the iterator aspects to work, here I am using the forward_iterator_tag as part of the parameter to the iterator and also we are returning a int value (which is what the stack is implementing as a linked list of integers, if you want to return another type just put in what ever your return type is.<\/p>\n<pre lang=\"cpp\">\r\n  class Iterator : public iterator<forward_iterator_tag, int>\r\n<\/pre>\n<p>here are function\/methods of a iterator to allow the stack iterator to go through the linked list (array of integers).<\/p>\n<pre lang=\"cpp\">\r\n  class Iterator : public iterator<forward_iterator_tag, int>\r\n  {\r\n    private: \r\n      \/\/ the intP is a integer pointer\r\n\tNode* intP;\r\n    public:\r\n\tIterator(Node* p) : intP(p) {}\r\n\t~Iterator() {}\r\n\r\n\t\/\/ assignment \r\n\tIterator& operator=(const Iterator& otherValue);\r\n\t\/\/ equal or not equal to\r\n\tbool operator==(const Iterator& otherValue);\r\n\tbool operator != (const Iterator& otherValue);\r\n\t\/\/ increment the pointer to the next value\r\n\tIterator& operator++();\r\n\t\/\/ increment the pointer to the next next etc value\r\n\tIterator& operator++(int);\r\n\t\r\n\t\/\/ return type here is a int because that is what I said at the top of the class setup\r\n\t\/\/ e.g. iterator<forward_iterator_tag, int>\r\n\tint& operator*();\r\n   };\r\n<\/pre>\n<p>of course we need to know when the begin and the end of the linked list is, this is the root and the last nodes, for a iterator to know where to start and end when you are going from start to end with using a iterator style of coding.  Here is a way of using the iterator to loop through the values added, below is code that setups up the stack (theStack) class with some values (5,10,3), and then using a for loop to go through the values within the linked list (this is where the iterator part comes in)<\/p>\n<pre lang=\"cpp\">\r\n    theStack stacky;\r\n    \r\n    stacky.addValue(5);\r\n    stacky.addValue(10);\r\n    stacky.addValue(3);\r\n    \r\n    printf(\"Value in the stack was\\n\");\r\n    for (theStack::Iterator theIterator= stacky.begin(); theIterator != stacky.end(); theIterator++)\r\n    {\r\n\t\/\/ (int) wrapping the return value into a int type, *theIterator getting the pointered to value\r\n\tprintf(\"%d\\n\", ((int)*theIterator));\r\n    }\r\n<\/pre>\n<p>in the full source code below is the implementations of the iterator and the stack<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <iterator>\r\n#include <stdio.h>\r\n\r\nusing namespace std;\r\n\r\nclass theStack\r\n{\r\n    private:\r\n    \/\/ forward declaration for the class structure below.\r\n    class Node;\r\n\r\n    \/\/ the pointers to the root nodes and the last node on the stack as such.\r\n    Node* rootNode;\r\n    Node* lastNode;\r\n    \r\n  public:\r\n      theStack(): rootNode(NULL), lastNode(NULL) {}\r\n      ~theStack() { delete rootNode; }\r\n      \r\n      \/\/ add objects to the DrawingObject\r\n      void addValue(int valueToAdd);\r\n  \r\n\r\n  class Iterator : public iterator<forward_iterator_tag, int>\r\n  {\r\n    private: \r\n      \/\/ the intP is a integer pointer\r\n\tNode* intP;\r\n    public:\r\n\tIterator(Node* p) : intP(p) {}\r\n\t~Iterator() {}\r\n\r\n\t\/\/ assignment \r\n\tIterator& operator=(const Iterator& otherValue);\r\n\t\/\/ equal or not equal to\r\n\tbool operator==(const Iterator& otherValue);\r\n\tbool operator != (const Iterator& otherValue);\r\n\t\/\/ increment the pointer to the next value\r\n\tIterator& operator++();\r\n\t\/\/ increment the pointer to the next next etc value\r\n\tIterator& operator++(int);\r\n\t\r\n\t\/\/ return type here is a int because that is what I said at the top of the class setup\r\n\t\/\/ e.g. iterator<forward_iterator_tag, int>\r\n\tint& operator*();\r\n   };\r\n      \r\n   \/\/ the begin and end of the iterator look up\r\n   Iterator begin();\r\n   Iterator end();\r\n\r\n   \/\/  here we define the Node class\r\n  private:\r\n    class Node {\r\n    public:\r\n\tNode(const int& val) : nextNode(NULL), nodeValue(val) {}\r\n\tint& getVal() {return(nodeValue);}\r\n\tNode* nextNode;\r\n    private:\r\n\tint nodeValue;\r\n    };\r\n\r\n};\r\n\r\nint main(int argc, char **argv) {\r\n    theStack stacky;\r\n    \r\n    stacky.addValue(5);\r\n    stacky.addValue(10);\r\n    stacky.addValue(3);\r\n    \r\n    printf(\"Value in the stack was\\n\");\r\n    for (theStack::Iterator theIterator= stacky.begin(); theIterator != stacky.end(); theIterator++)\r\n    {\r\n\t\/\/ (int) wrapping the return value into a int type, *theIterator getting the pointered to value\r\n\tprintf(\"%d\\n\", ((int)*theIterator));\r\n    }\r\n    return 0;\r\n}\r\n\r\n\r\nvoid theStack::addValue(int valueToAdd)\r\n{\r\n    if (rootNode == NULL)\r\n    {\r\n\trootNode = new Node(valueToAdd);\r\n\tlastNode = rootNode;\r\n    }else\r\n    {\r\n\tNode* p = new Node(valueToAdd);\r\n\tlastNode->nextNode = p;\r\n\tlastNode =p;\r\n    }\r\n}\r\n\r\n\r\n\/\/ the definitions of the class theStack\r\n\/\/ Iterator class implementations \r\ntheStack::Iterator& theStack::Iterator::operator=(const Iterator& otherValue)\r\n{\r\n  intP = otherValue.intP;\r\n  return (*this);\r\n}\r\n\r\nbool theStack::Iterator::operator==(const Iterator& otherValue)\r\n{\r\n    return (intP == otherValue.intP);\r\n}\r\n\r\nbool theStack::Iterator::operator != (const Iterator& otherValue)\r\n{\r\n    return (intP != otherValue.intP);\r\n}\r\n\r\n\/\/ increment the pointer to the next value\r\ntheStack::Iterator& theStack::Iterator::operator++()\r\n{\r\n    if (intP != NULL)\r\n      intP = intP->nextNode;\r\n    return (*this);\r\n}\r\n\r\n\/\/ if the loop has a increment by more than 1 then just increment the value still by one.\r\n\/\/ if you are implementing another stack then you could get to the next next next etc nodes\r\ntheStack::Iterator& theStack::Iterator::operator++(int)\r\n{\r\n    if (intP != NULL)\r\n      intP = intP->nextNode;\r\n    return (*this);\r\n}\r\n\r\nint& theStack::Iterator::operator*()\r\n{\r\n    return(intP->getVal());\r\n}\r\n\r\ntheStack::Iterator theStack::begin()\r\n{\r\n  return(Iterator(rootNode));\r\n}\r\n\r\ntheStack::Iterator theStack::end()\r\n{\r\n  return(Iterator(NULL));\r\n}\r\n<\/pre>\n<p>Output would be<\/p>\n<pre lang=\"bash\">\r\nValue in the stack was\r\n5\r\n10\r\n3\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An iterator is when you implement a abstract way of cycling through data. I have below created a internal iterator wrapper that will allow the class to implement a iterator for that class. Below the class is kinder like a stack where you place one item at the root of the stack and then place &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/27\/iterator-wrapping-around-a-class\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">iterator &#8211; wrapping around a class<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[194,51,193],"class_list":["post-909","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-forward_iterator_tag","tag-iterator","tag-wrap"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"predecessor-version":[{"id":911,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/909\/revisions\/911"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}