{"id":62215,"date":"2021-05-15T23:10:05","date_gmt":"2021-05-16T03:10:05","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/sort-objects-stored-in-deque-programming-code-examples-c-std-algorithms\/"},"modified":"2021-05-15T23:10:05","modified_gmt":"2021-05-16T03:10:05","slug":"sort-objects-stored-in-deque-programming-code-examples-c-std-algorithms","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=62215","title":{"rendered":"Sort objects stored in deque #Programming Code Examples #C++ #Std Algorithms"},"content":{"rendered":"<pre style='padding:10px;font-size:15px'>\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#include &lt;iostream&gt;\n#include &lt;string&gt;\n#include &lt;deque&gt;\n#include &lt;set&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\n\n\n\/* class Person\n *\/\nclass Person {\n  private:\n    string fn;    \/\/ first name\n    string ln;    \/\/ last name\n  public:\n    Person() {\n    }\n    Person(const string&amp; f, const string&amp; n)\n     : fn(f), ln(n) {\n    }\n    string firstname() const;\n    string lastname() const;\n    \/\/ ...\n};\n\ninline string Person::firstname() const {\n    return fn;\n}\n\ninline string Person::lastname() const {\n    return ln;\n}\n\nostream&amp; operator&lt;&lt; (ostream&amp; s, const Person&amp; p)\n{\n    s &lt;&lt; &quot;[&quot; &lt;&lt; p.firstname() &lt;&lt; &quot; &quot; &lt;&lt; p.lastname() &lt;&lt; &quot;]&quot;;\n    return s;\n}\n\n\n\/* binary function predicate:\n * - returns whether a person is less than another person\n *\/\nbool personSortCriterion (const Person&amp; p1, const Person&amp; p2)\n{\n    \/* a person is less than another person\n     * - if the last name is less\n     * - if the last name is equal and the first name is less\n     *\/\n    return p1.lastname()&lt;p2.lastname() ||\n           (p1.lastname()==p2.lastname() &amp;&amp;\n            p1.firstname()&lt;p2.firstname());\n}\n\nint main()\n{\n    \/\/ create some persons\n    Person p1(&quot;nicolai&quot;,&quot;josuttis&quot;);\n    Person p2(&quot;ulli&quot;,&quot;josuttis&quot;);\n    Person p3(&quot;anica&quot;,&quot;josuttis&quot;);\n    Person p4(&quot;lucas&quot;,&quot;josuttis&quot;);\n    Person p5(&quot;lucas&quot;,&quot;otto&quot;);\n    Person p6(&quot;lucas&quot;,&quot;arm&quot;);\n    Person p7(&quot;anica&quot;,&quot;holle&quot;);\n\n    \/\/ insert person into collection coll\n    deque&lt;Person&gt; coll;\n    coll.push_back(p1);\n    coll.push_back(p2);\n    coll.push_back(p3);\n    coll.push_back(p4);\n    coll.push_back(p5);\n    coll.push_back(p6);\n    coll.push_back(p7);\n\n    \/\/ print elements\n    cout &lt;&lt; &quot;deque before sort():&quot; &lt;&lt; endl;\n    deque&lt;Person&gt;::iterator pos;\n    for (pos = coll.begin(); pos != coll.end(); ++pos) {\n        cout &lt;&lt; *pos &lt;&lt; endl;\n    }\n\n    \/\/ sort elements\n    sort(coll.begin(),coll.end(),    \/\/ range\n         personSortCriterion);       \/\/ sort criterion\n\n    \/\/ print elements\n    cout &lt;&lt; &quot;deque after sort():&quot; &lt;&lt; endl;\n    for (pos = coll.begin(); pos != coll.end(); ++pos) {\n        cout &lt;&lt; *pos &lt;&lt; endl;\n    }\n}\n\n\/*\ndeque before sort():\n[nicolai josuttis]\n[ulli josuttis]\n[anica josuttis]\n[lucas josuttis]\n[lucas otto]\n[lucas arm]\n[anica holle]\ndeque after sort():\n[lucas arm]\n[anica holle]\n[anica josuttis]\n[lucas josuttis]\n[nicolai josuttis]\n[ulli josuttis]\n[lucas otto]\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=10192<br \/> Categories:Programming Code Examples, C++, Std Algorithms<br \/>Tags:C++Std Algorithms<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=62215\">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-62215","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":10091,"url":"http:\/\/bangla.sitestree.com\/?p=10091","url_meta":{"origin":62215,"position":0},"title":"Sort objects stored in deque","author":"","date":"July 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":10552,"url":"http:\/\/bangla.sitestree.com\/?p=10552","url_meta":{"origin":62215,"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":27255,"url":"http:\/\/bangla.sitestree.com\/?p=27255","url_meta":{"origin":62215,"position":2},"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":10094,"url":"http:\/\/bangla.sitestree.com\/?p=10094","url_meta":{"origin":62215,"position":3},"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":27160,"url":"http:\/\/bangla.sitestree.com\/?p=27160","url_meta":{"origin":62215,"position":4},"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":10066,"url":"http:\/\/bangla.sitestree.com\/?p=10066","url_meta":{"origin":62215,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/62215","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=62215"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/62215\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=62215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=62215"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=62215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}