{"author_name":"rooks","cat":"October Challenge 2010","comments":[{"author_name":"","time":"October 29, 2010 1:58 am","epoch":1288335480,"text":"The typeof operator is quite a new C++ feature.<br \/>\n Would this work? :<br \/>\n for(k.iterator it=k.begin(); it!=k.end(); ++it)","spam":"N"},{"author_name":"","time":"October 29, 2010 8:16 pm","epoch":1288401360,"text":"Not only would it work, but it&#8217;d be the appropriate C++\/STL way of iterating over a collection. Though I&#8217;m sure rooks&#8217; foreach_ method is nothing more than a macro that expands to exactly that&#8230;","spam":"N"}],"epoch":1288324680,"likes":1,"metadata":{"p_key":"69075","p_author":"rooks","p_authorkey":"0","p_urlkey":"104878","p_title":"Foreach in c++","p_cat":"October Challenge 2010","p_event":"LD18","p_time":"1288324680","p_likes":"1","p_comments":"3","p_status":"UPD5","us_key":null,"us_name":null,"us_username":null,"event_start":"1282262400","event_key":"4","event_name":"LD18"},"text":"<p>Hi<\/p>\n <p>I created simple helper that might help few of you who use c++ with STL. It adds foreach to the c++ that traverses over all of the STL container, so the code is more readable, like in this example:<\/p>\n <pre>#include &lt;cstdio&gt;<\/pre>\n <pre>#include &lt;vector&gt;<\/pre>\n <pre>#include \"foreach.h\"<\/pre>\n <pre><\/pre>\n <pre>int main()<\/pre>\n <pre>{<\/pre>\n <pre>    \/\/ make int vector and fill it<\/pre>\n <pre>    vector&lt;int&gt; k;<\/pre>\n <pre>    for (int i=0; i&lt;10; ++i) k.push_back(i);<\/pre>\n <pre><\/pre>\n <pre>    \/\/ show what the upper loop filled<\/pre>\n <pre>    foreach_ (it, k) printf(\"%i \",(*it));<\/pre>\n <pre>    printf(\"\\n\");<\/pre>\n <pre><\/pre>\n <pre>    \/\/ show all the data, but get rid of 4<\/pre>\n <pre>    \/\/ http:\/\/en.wikipedia.org\/wiki\/Tetraphobia :)<\/pre>\n <pre>    foreachdel_ (it, k)<\/pre>\n <pre>    {<\/pre>\n <pre>        if (*it == 4) it=k.erase(it);<\/pre>\n <pre>        printf(\"%i \",(*it));<\/pre>\n <pre>    }<\/pre>\n <pre>    printf(\"\\n\");<\/pre>\n <pre><\/pre>\n <pre>    return 0;<\/pre>\n <pre>}<\/pre>\n <p>Will result in following:<\/p>\n <pre>0 1 2 3 4 5 6 7 8 9<\/pre>\n <pre>0 1 2 3 5 6 7 8 9<\/pre>\n <p>More on my blog: \u00a0<a href=\"http:\/\/pleasanthacking.com\/2010\/06\/17\/foreach-in-cpp\/\">http:\/\/pleasanthacking.com\/2010\/06\/17\/foreach-in-cpp\/<\/a><\/p>\n <p>I hope some of you will find it useful during compos <img src=\"http:\/\/ludumdare.com\/compo\/wp-includes\/images\/smilies\/simple-smile.png\" alt=\":)\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n \n <p>Tags: <a href=\"http:\/\/ludumdare.com\/compo\/tag\/c\/\" rel=\"tag\">C++<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/goodies\/\" rel=\"tag\">goodies<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/libraries\/\" rel=\"tag\">libraries<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/libs\/\" rel=\"tag\">libs<\/a>, <a href=\"http:\/\/ludumdare.com\/compo\/tag\/tools\/\" rel=\"tag\">tools<\/a><\/p>","time":"October 28th, 2010 10:58 pm","title":"Foreach in c++"}