{"id":27255,"date":"2021-05-15T11:16:19","date_gmt":"2021-05-15T15:16:19","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/call-member-function-for-each-element-in-vector-programming-code-examples-c-vector\/"},"modified":"2021-05-15T11:16:19","modified_gmt":"2021-05-15T15:16:19","slug":"call-member-function-for-each-element-in-vector-programming-code-examples-c-vector","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=27255","title":{"rendered":"Call member function for each element in vector #Programming Code Examples #C++ #Vector"},"content":{"rendered":"<pre style='font-size:15px;padding:10px'>\n\/* The following code example is taken from the book\n * &quot;The C++ Standard Library - A Tutorial and Reference&quot;\n * by Nicolai M. Josuttis, Addison-Wesley, 1999\n *\n * (C) Copyright Nicolai M. Josuttis 1999.\n * Permission to copy, use, modify, sell and distribute this software\n * is granted provided this copyright notice appears in all copies.\n * This software is provided &quot;as is&quot; without express or implied\n * warranty, and with no claim as to its suitability for any purpose.\n *\/\n\/\/#define mem_fun1 mem_fun\n#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\n#include &lt;functional&gt;\n\n\nclass Person {\n  private:\n    std::string name;\n  public:\n    \/\/...\n    void print () const {\n        std::cout &lt;&lt; name &lt;&lt; std::endl;\n    }\n    void printWithPrefix (std::string prefix) const {\n        std::cout &lt;&lt; prefix &lt;&lt; name &lt;&lt; std::endl;\n    }\n};\n\nvoid foo (const std::vector&lt;Person&gt;&amp; coll)\n{\n    using std::for_each;\n    using std::bind2nd;\n    using std::mem_fun_ref;\n\n    \/\/ call member function print() for each element\n    for_each (coll.begin(), coll.end(), mem_fun_ref(&amp;Person::print));\n\n    \/\/ call member function printWithPrefix() for each element\n    \/\/ - &quot;person: &quot; is passed as an argument to the member function\n    for_each (coll.begin(), coll.end(),bind2nd(mem_fun_ref(&amp;Person::printWithPrefix),&quot;person: &quot;));\n}\n\n\nvoid ptrfoo (const std::vector&lt;Person*&gt;&amp; coll)\n                                   \/\/ ^^^ pointer !\n{\n    using std::for_each;\n    using std::bind2nd;\n    using std::mem_fun;\n\n    \/\/ call member function print() for each referred object\n    for_each (coll.begin(), coll.end(),\n              mem_fun(&amp;Person::print));\n\n    \/\/ call member function printWithPrefix() for each referred object\n    \/\/ - &quot;person: &quot; is passed as an argument to the member function\n    for_each (coll.begin(), coll.end(),bind2nd(mem_fun(&amp;Person::printWithPrefix),&quot;person: &quot;));\n}\n\n\nint main()\n{\n    std::vector&lt;Person&gt; coll(5);\n    foo(coll);\n\n    std::vector&lt;Person*&gt; coll2;\n    coll2.push_back(new Person);\n    ptrfoo(coll2);\n}\n\n  \/*\n\n\n\n\n\nperson:\nperson:\nperson:\nperson:\nperson:\n\nperson:\n\n *\/\n<\/pre>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10186<br \/> Categories:Programming Code Examples, C++, Vector<br \/>Tags:C++Vector<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/* The following code example is taken from the book * &quot;The C++ Standard Library &#8211; A Tutorial and Reference&quot; * 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=27255\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-27255","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10066,"url":"http:\/\/bangla.sitestree.com\/?p=10066","url_meta":{"origin":27255,"position":0},"title":"Call member function for each element in vector","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":27255,"position":1},"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":62215,"url":"http:\/\/bangla.sitestree.com\/?p=62215","url_meta":{"origin":27255,"position":2},"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":[]},{"id":27160,"url":"http:\/\/bangla.sitestree.com\/?p=27160","url_meta":{"origin":27255,"position":3},"title":"C++ Template Example #Programming Code Examples #Java\/J2EE\/J2ME #Ajax","author":"Author-Check- Article-or-Video","date":"May 12, 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":10094,"url":"http:\/\/bangla.sitestree.com\/?p=10094","url_meta":{"origin":27255,"position":4},"title":"Use sorting criterion in sort function","author":"","date":"July 30, 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":62217,"url":"http:\/\/bangla.sitestree.com\/?p=62217","url_meta":{"origin":27255,"position":5},"title":"Use sorting criterion in sort function #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\/27255","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\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=27255"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/27255\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=27255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=27255"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=27255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}