{"id":10066,"date":"2015-07-27T01:40:07","date_gmt":"2015-07-27T05:40:07","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/?p=10066"},"modified":"2015-08-04T10:15:01","modified_gmt":"2015-08-04T14:15:01","slug":"%e0%a6%ad%e0%a7%87%e0%a6%95%e0%a7%8d%e0%a6%9f%e0%a6%b0-%e0%a6%8f%e0%a6%b2%e0%a6%bf%e0%a6%ae%e0%a7%87%e0%a6%a8%e0%a7%8d%e0%a6%9f-%e0%a6%8f%e0%a6%b0-%e0%a6%b8%e0%a7%87%e0%a6%b2-%e0%a6%a8%e0%a6%be","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=10066","title":{"rendered":"Call member function for each element in vector"},"content":{"rendered":"<pre>\/* The following code example is taken from the book\r\n* \"The C++ Standard Library - A Tutorial and Reference\"\r\n* by Nicolai M. Josuttis, Addison-Wesley, 1999\r\n*\r\n* (C) Copyright Nicolai M. Josuttis 1999.\r\n* Permission to copy, use, modify, sell and distribute this software\r\n* is granted provided this copyright notice appears in all copies.\r\n* This software is provided \"as is\" without express or implied\r\n* warranty, and with no claim as to its suitability for any purpose.\r\n*\/\r\n\/\/#define mem_fun1 mem_fun\r\n#include &lt;iostream&gt;\r\n#include &lt;vector&gt;\r\n#include &lt;string&gt;\r\n#include &lt;algorithm&gt;\r\n#include &lt;functional&gt;<\/pre>\n<pre>class Person {\r\nprivate:\r\n\u00a0\u00a0 std::string name;\r\npublic:\r\n\u00a0\u00a0 \/\/...\r\n\u00a0\u00a0 void print () const {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 std::cout &lt;&lt; name &lt;&lt; std::endl;\r\n\u00a0\u00a0 }\r\n\u00a0\u00a0 void printWithPrefix (std::string prefix) const {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 std::cout &lt;&lt; prefix &lt;&lt; name &lt;&lt; std::endl;\r\n\u00a0\u00a0 }\r\n};\r\n<\/pre>\n<pre>void foo (const std::vector&lt;Person&gt;&amp; coll)\r\n{\r\n\u00a0\u00a0 using std::for_each;\r\n\u00a0\u00a0 using std::bind2nd;\r\n\u00a0\u00a0 using std::mem_fun_ref;\r\n<\/pre>\n<pre>\u00a0\u00a0 \/\/ call member function print() for each element\r\n\u00a0\u00a0 for_each (coll.begin(), coll.end(), mem_fun_ref(&amp;Person::print));\r\n<\/pre>\n<pre>\u00a0\u00a0 \/\/ call member function printWithPrefix() for each element\r\n\u00a0\u00a0 \/\/ - \"person: \" is passed as an argument to the member function<\/pre>\n<pre>\u00a0\u00a0 for_each (coll.begin(), coll.end(),bind2nd(mem_fun_ref(&amp;Person::printWithPrefix),\"person: \"));\r\n}<\/pre>\n<pre><\/pre>\n<pre>void ptrfoo (const std::vector&lt;Person*&gt;&amp; coll)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ ^^^ pointer !<\/pre>\n<pre>{\r\n\u00a0\u00a0 using std::for_each;\r\n\u00a0\u00a0 using std::bind2nd;\r\n\u00a0\u00a0 using std::mem_fun;\r\n<\/pre>\n<pre>\u00a0\u00a0 \/\/ call member function print() for each referred object\r\n\u00a0\u00a0 for_each (coll.begin(), coll.end(),<\/pre>\n<pre>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 mem_fun(&amp;Person::print));\r\n<\/pre>\n<pre>\u00a0\u00a0 \/\/ call member function printWithPrefix() for each referred object\r\n\u00a0\u00a0 \/\/ - \"person: \" is passed as an argument to the member function<\/pre>\n<pre>\u00a0\u00a0 for_each (coll.begin(), coll.end(),bind2nd(mem_fun(&amp;Person::printWithPrefix),\"person: \"));\r\n} \r\n\u00a0\r\nint main()\r\n{<\/pre>\n<pre>\u00a0\u00a0 std::vector&lt;Person&gt; coll(5);\r\n\u00a0\u00a0 foo(coll); \r\n\u00a0\u00a0 std::vector&lt;Person*&gt; coll2;\r\n\u00a0\u00a0 coll2.push_back(new Person);\r\n\u00a0\u00a0 ptrfoo(coll2);\r\n} \r\n\/*<\/pre>\n<pre>person:\r\nperson:\r\nperson:\r\nperson:\r\nperson:\r\nperson: \r\n\r\n*\/<\/pre>\n<pre><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/* The following code example is taken from the book * &#8220;The C++ Standard Library &#8211; A Tutorial and Reference&#8221; * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=10066\">Continue reading<\/a><\/p>\n","protected":false},"author":130,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1420,1439,1417],"tags":[1435,508,773,1436,606,600,1433,1434],"class_list":["post-10066","post","type-post","status-publish","format-standard","hentry","category-c","category-c----","category-code-programming-samples--","tag-call-member","tag-element","tag-function","tag-vector","tag-606","tag-600","tag-1433","tag-1434","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":27255,"url":"http:\/\/bangla.sitestree.com\/?p=27255","url_meta":{"origin":10066,"position":0},"title":"Call member function for each element in vector #Programming Code Examples #C++ #Vector","author":"Author-Check- Article-or-Video","date":"May 15, 2021","format":false,"excerpt":"\/* The following code example is taken from the book * \"The C++ Standard Library - A Tutorial and Reference\" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10064,"url":"http:\/\/bangla.sitestree.com\/?p=10064","url_meta":{"origin":10066,"position":1},"title":"Use bitset with enum together","author":"","date":"July 27, 2015","format":false,"excerpt":"\/*\u00a0The\u00a0following\u00a0code\u00a0example\u00a0is\u00a0taken\u00a0from\u00a0the\u00a0book \u00a0*\u00a0\"The\u00a0C++\u00a0Standard\u00a0Library\u00a0-\u00a0A\u00a0Tutorial\u00a0and\u00a0Reference\" \u00a0*\u00a0by\u00a0Nicolai\u00a0M.\u00a0Josuttis,\u00a0Addison-Wesley,\u00a01999 \u00a0* \u00a0*\u00a0(C)\u00a0Copyright\u00a0Nicolai\u00a0M.\u00a0Josuttis\u00a01999. \u00a0*\u00a0Permission\u00a0to\u00a0copy,\u00a0use,\u00a0modify,\u00a0sell\u00a0and\u00a0distribute\u00a0this\u00a0software \u00a0*\u00a0is\u00a0granted\u00a0provided\u00a0this\u00a0copyright\u00a0notice\u00a0appears\u00a0in\u00a0all\u00a0copies. \u00a0*\u00a0This\u00a0software\u00a0is\u00a0provided\u00a0\"as\u00a0is\"\u00a0without\u00a0express\u00a0or\u00a0implied \u00a0*\u00a0warranty,\u00a0and\u00a0with\u00a0no\u00a0claim\u00a0as\u00a0to\u00a0its\u00a0suitability\u00a0for\u00a0any\u00a0purpose. \u00a0*\/ #include\u00a0<bitset> #include\u00a0<iostream> using\u00a0namespace\u00a0std; int\u00a0main() { \u00a0\u00a0\u00a0\u00a0\/*\u00a0enumeration\u00a0type\u00a0for\u00a0the\u00a0bits \u00a0\u00a0\u00a0\u00a0\u00a0*\u00a0-\u00a0each\u00a0bit\u00a0represents\u00a0a\u00a0color \u00a0\u00a0\u00a0\u00a0\u00a0*\/ \u00a0\u00a0\u00a0\u00a0enum\u00a0Color\u00a0{\u00a0red,\u00a0yellow,\u00a0green,\u00a0blue,\u00a0white,\u00a0black,\u00a0\/\/..., \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0numColors\u00a0}; \u00a0\u00a0\u00a0\u00a0\/\/\u00a0create\u00a0bitset\u00a0for\u00a0all\u00a0bits\/colors \u00a0\u00a0\u00a0\u00a0bitset<numColors>\u00a0usedColors; \u00a0\u00a0\u00a0\u00a0\/\/\u00a0set\u00a0bits\u00a0for\u00a0two\u00a0colors \u00a0\u00a0\u00a0\u00a0usedColors.set(red); \u00a0\u00a0\u00a0\u00a0usedColors.set(blue); \u00a0\u00a0\u00a0\u00a0\/\/\u00a0print\u00a0some\u00a0bitset\u00a0data \u00a0\u00a0\u00a0\u00a0cout\u00a0<<\u00a0\"bitfield\u00a0of\u00a0used\u00a0colors:\u00a0\u00a0\u00a0\"\u00a0<<\u00a0usedColors \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<<\u00a0endl; \u00a0\u00a0\u00a0\u00a0cout\u00a0<<\u00a0\"number\u00a0\u00a0\u00a0of\u00a0used\u00a0colors:\u00a0\u00a0\u00a0\"\u00a0<<\u00a0usedColors.count() \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<<\u00a0endl; \u00a0\u00a0\u00a0\u00a0cout\u00a0<<\u00a0\"bitfield\u00a0of\u00a0unused\u00a0colors:\u00a0\"\u00a0<<\u00a0~usedColors \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<<\u00a0endl; \u00a0\u00a0\u00a0\u00a0\/\/\u00a0if\u00a0any\u00a0color\u00a0is\u00a0used \u00a0\u00a0\u00a0\u00a0if\u00a0(usedColors.any())\u00a0{ \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0loop\u00a0over\u00a0all\u00a0colors \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0for\u00a0(int\u00a0c\u00a0=\u00a00;\u00a0c\u00a0<\u00a0numColors;\u00a0++c)\u00a0{ \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0if\u00a0the\u00a0actual\u00a0color\u00a0is\u00a0used \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0(usedColors[(Color)c])\u00a0{ \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/... \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0} \u00a0\u00a0\u00a0\u00a0} } \/*\u00a0 bitfield\u00a0of\u00a0used\u00a0colors:\u00a0\u00a0\u00a0001001 number\u00a0\u00a0\u00a0of\u00a0used\u00a0colors:\u00a0\u00a0\u00a02 bitfield\u00a0of\u00a0unused\u00a0colors:\u00a0110110 \u00a0*\/","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"http:\/\/bangla.sitestree.com\/?cat=1420"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10070,"url":"http:\/\/bangla.sitestree.com\/?p=10070","url_meta":{"origin":10066,"position":2},"title":"Print minimum, maximum, and sum of the valarray","author":"","date":"July 27, 2015","format":false,"excerpt":"\/* The following code example is taken from the book * \"The C++ Standard Library - A Tutorial and Reference\" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"http:\/\/bangla.sitestree.com\/?cat=1420"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10552,"url":"http:\/\/bangla.sitestree.com\/?p=10552","url_meta":{"origin":10066,"position":3},"title":"C++ Template Example","author":"","date":"August 29, 2015","format":false,"excerpt":"\/* The following code example is taken from the book \u00a0* \"The C++ Standard Library - A Tutorial and Reference\" \u00a0* by Nicolai M. Josuttis, Addison-Wesley, 1999 \u00a0* \u00a0* (C) Copyright Nicolai M. Josuttis 1999. \u00a0* Permission to copy, use, modify, sell and distribute this software \u00a0* is granted provided\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"http:\/\/bangla.sitestree.com\/?cat=1420"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10073,"url":"http:\/\/bangla.sitestree.com\/?p=10073","url_meta":{"origin":10066,"position":4},"title":"valarray slice","author":"","date":"July 27, 2015","format":false,"excerpt":"\/* The following code example is taken from the book * \"The C++ Standard Library - A Tutorial and Reference\" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided\u2026","rel":"","context":"In &quot;C++ \u0964 \u09b8\u09bf \u09aa\u09cd\u09b2\u09be\u09b8 \u09aa\u09cd\u09b2\u09be\u09b8&quot;","block_context":{"text":"C++ \u0964 \u09b8\u09bf \u09aa\u09cd\u09b2\u09be\u09b8 \u09aa\u09cd\u09b2\u09be\u09b8","link":"http:\/\/bangla.sitestree.com\/?cat=1439"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":62215,"url":"http:\/\/bangla.sitestree.com\/?p=62215","url_meta":{"origin":10066,"position":5},"title":"Sort objects stored in deque #Programming Code Examples #C++ #Std Algorithms","author":"Author-Check- Article-or-Video","date":"May 15, 2021","format":false,"excerpt":"\/* The following code example is taken from the book * \"The C++ Standard Library - A Tutorial and Reference\" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10066","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/130"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10066"}],"version-history":[{"count":4,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10066\/revisions"}],"predecessor-version":[{"id":10617,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/10066\/revisions\/10617"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10066"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}