{"id":1819,"date":"2014-06-10T21:49:58","date_gmt":"2014-06-10T20:49:58","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1819"},"modified":"2014-06-17T11:50:41","modified_gmt":"2014-06-17T10:50:41","slug":"vectors-and-for_each","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2014\/06\/10\/vectors-and-for_each\/","title":{"rendered":"vectors and for_each"},"content":{"rendered":"<p>A friend of mine asked how to use the newer for_each and is it similar to the foreach in php, so here is the example that I gave and thus doing it online as well.<\/p>\n<p>To start with the foreach (php version <a href=\"http:\/\/us2.php.net\/\/manual\/en\/control-structures.foreach.php\">here<\/a>) does loop over data and so does the for_each in c++, but the main difference is that the for_each in c++ is basically a for loop<\/p>\n<pre lang=\"cpp\">\r\nfor (; first!=last;++first)\r\n   functionCall(*first);\r\n<\/pre>\n<p>where the functionCall is the parameter passed in to do something with that part of the data.<\/p>\n<p>The great thing with c++ is that you are able to use either a method, structure etc as long as it is able to output a variable then we are good.<\/p>\n<p>So here is a example, please note that I am loading up a vector<int> e.g. only functions that have a single int make sense.<\/p>\n<pre lang=\"cpp\">\r\nvoid printNumber (int i) {\r\n\tcout << \"Print the number from a function : \" << i << \"\\n\";\r\n}\r\n\r\nfor_each(v.begin(), v.end(), printNumber);\r\n<\/pre>\n<p>here we are using the for_each (where the v is a vector) passing in the start and end of the vector with a function as the last parameter.<\/p>\n<p>and here<\/p>\n<pre lang=\"cpp\">\r\n\/\/ create a object that can use the operator() to output value passed in.\r\nstruct structToOutput {\r\n\tvoid operator() (int i) {\r\n\t\tcout << \"Struct to output : \" << i << \"\\n\";\r\n\t}\r\n} structToOutputObject;\r\n\r\nfor_each(v.begin(), v.end(), structToOutputObject);\r\n<\/pre>\n<p>will output the values differently but in essence still able to access the values.<\/p>\n<p>Here is the code in full<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <vector>\r\n#include <algorithm>\r\n\r\nusing namespace std;\r\n\r\nvoid printNumber (int i) {\r\n\tcout << \"Print the number from a function : \" << i << \"\\n\";\r\n}\r\n\r\n\/\/ create a object that can use the operator() to output value passed in.\r\nstruct structToOutput {\r\n\tvoid operator() (int i) {\r\n\t\tcout << \"Struct to output : \" << i << \"\\n\";\r\n\t}\r\n} structToOutputObject;\r\n\r\nint main(int argc, char* argv[])\r\n{\r\n\t\/\/ lets load up some dummy data.\r\n\tvector<int> v;\r\n\tfor (int i =0; i< 10; i++)\r\n\t\tv.push_back(i);\r\n\r\n\t\/\/ run through the vector using a standard function with parameter\r\n\tfor_each(v.begin(), v.end(), printNumber);\r\n\t\/\/ output using a operator() method\r\n\tfor_each(v.begin(), v.end(), structToOutputObject);\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>with the output being<\/p>\n<pre lang=\"bash\">\r\nPrint the number from a function : 0\r\nPrint the number from a function : 1\r\nPrint the number from a function : 2\r\nPrint the number from a function : 3\r\nPrint the number from a function : 4\r\nPrint the number from a function : 5\r\nPrint the number from a function : 6\r\nPrint the number from a function : 7\r\nPrint the number from a function : 8\r\nPrint the number from a function : 9\r\nStruct to output : 0\r\nStruct to output : 1\r\nStruct to output : 2\r\nStruct to output : 3\r\nStruct to output : 4\r\nStruct to output : 5\r\nStruct to output : 6\r\nStruct to output : 7\r\nStruct to output : 8\r\nStruct to output : 9\r\n<\/pre>\n<p>How to compile with g++<\/p>\n<pre lang=\"bash\">\r\ng++ <filename> -o <outfilename> -std=c++11\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A friend of mine asked how to use the newer for_each and is it similar to the foreach in php, so here is the example that I gave and thus doing it online as well. To start with the foreach (php version here) does loop over data and so does the for_each in c++, but &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2014\/06\/10\/vectors-and-for_each\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">vectors and for_each<\/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":[],"class_list":["post-1819","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1819","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=1819"}],"version-history":[{"count":2,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":1822,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1819\/revisions\/1822"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}