C++ "Scrolling" through items in an stl::map -
I have created a method of scrolling around the map of the items, so that if the end is reached, then the method
MyMap :: const_iterator = myMap.find (myKey);
If (this == myMap.end ()) 0; If (next) {this ++; If (this == myMap.end ()) {this = myMap.begin (); }} And {if (this is == myMap.begin ()) {this = myMap.end (); } this -; }
You can do this with a template as told by the previous poster It may be cumbersome from the point of view that it never reaches to the end so that the user should control it anyhow. I'm assuming that you have a good reason, maybe creating some round robin behaviors
#include & lt; Iostream & gt; #include & lt; String & gt; # Include & lt; Vector & gt; # Include & lt; Set & gt; # Include & lt; Map & gt; using namespace std; Template & lt; Class T & gt; Class Scroll Itater {Public: Scroll Iterator (T & amp; Micrate, TypeName :: Etherator Pauses): CTR (Micro), This (Pause) {} Scroll Iterator Operator ++ () {if (++ it == ctr .end ()) {this = ctr.begin (); } Return * This; } Bool Operator! = (Constrict Scroll Iterator & amp; RA) const {Return (it-> It! = Rhs.it); } Bool Operator! = (Const Typedem T :: Constiterrator & rhsIT) const {Return (it-> =!; RhsIT;); } Typename T :: value_type operator * () const {return * it; Private: T & amp; CTR; Typename T :: Iterator; }; Int main (int argc, char * argv []) {vector & lt; Int & gt; V; V.push_back (2); V.push_back (3); V.push_back (5); V.push_back (7); Int i = 0; (ScrollIterator int>> (v, v.begin ()); it! = V.end () & amp; i <10; ++ i, ++ this) {cout & lt; & Lt; "Vector =" & lt; & Lt; I & lt; & Lt; "Value:" & lt; & Lt; * This is & lt; & Lt; "\ N"; } Set & lt; String & gt; S; S.insert ("c"); S.insert ("one"); S.insert ("b"); I = 0; (ScrollIterator & lt; set & lt; string & gt; & gt; it (S, S.Began ()); it! = S.end () & amp; i & lt; 10; ++ i, ++ this) {cout & lt; & Lt; "Set =" & lt; & Lt; I & lt; & Lt; "Value:" & lt; & Lt; * This is & lt; & Lt; "\ N"; } Map & lt; String, int & gt; Y; Y ["z"] = 10; Y ["Y"] = 20; Y ["x"] = 30; I = 0; (ScrollIterator & lt; map & lt; string, int & gt; & gt; this (y, y.begin ()); it! = Y.end () & amp; amp; i & lt; 10; ++ I, ++ this) {cout & lt; & Lt; "Map =" & lt; & Lt; I & lt; & Lt; "Iterator:" & lt; & Lt; (* this). First & lt; & Lt; "=" & Lt; & Lt; (* This) .second & lt; & Lt; "\ N"; } Return 1; }
Comments
Post a Comment