{"id":17388,"date":"2022-10-24T11:10:02","date_gmt":"2022-10-24T05:40:02","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/"},"modified":"2022-10-24T11:10:02","modified_gmt":"2022-10-24T05:40:02","slug":"solved-task-with-inserting-elements-to-list-tough-a-little","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/","title":{"rendered":"[Solved] Task with inserting elements to list &#8211; tough a little"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-72306568\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"72306568\" data-parentid=\"72294123\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>One wrong thing in your check function: you only check the names in the current team to skip that, but you should skip any name that was already put in a team.<\/p>\n<p>I already gave you an alternative on another question, so the check function isn&#8217;t needed (erasing from the list).<\/p>\n<p>Also, when iterating over a list, you should take into account that when you reach end, you must continue at begin. Adding the names a 100 times isn&#8217;t a good alternative.<\/p>\n<p>Hints:<\/p>\n<ul>\n<li>try to put in comments what your code is supposed to do so you and we can see why the code doesn&#8217;t work as expected.<\/li>\n<li>When possible, use const reference to pass data-structures to functions<\/li>\n<li>For the number of next shifts: it-&gt;length() works too, no need to look for the name.<\/li>\n<li>You can easily avoid the code duplication, the only difference is one number.<\/li>\n<li>i and l have the same meaning and when you iterate i from 0 to &lt; k, they become the same.<\/li>\n<\/ul>\n<p><strong>EDIT after changing code in the question based on my comments<\/strong><\/p>\n<p>Code how I would do it based on your edited code (there are two names switched with the expected output), <code>make_team()<\/code> and <code>Distribution()<\/code> are working together on the data, so they could be joined in a class:<\/p>\n<pre><code>#include &lt;iostream&gt;\n#include &lt;list&gt;\n#include &lt;set&gt;\n#include &lt;string&gt;\n#include &lt;vector&gt;\n\n\/\/ data structure for result\ntypedef std::vector&lt;std::set&lt;std::string&gt;&gt; ResultType;\n\n\/\/ put players in one team according to shift algorithm (shifts is length of name of last player) to make more random\n\/\/ list_players and it_players are references and are being changed in this function\nstd::set&lt;std::string&gt; make_team(std::list&lt;std::string&gt;&amp; list_players, std::list&lt;std::string&gt;::iterator&amp; it_players,int size)\n{\n  std::set&lt;std::string&gt; team;\n  int number_shifts = 0;\n  int number_of_members_in_team = 0;\n  while (number_of_members_in_team &lt; size) \n  {\n    \/\/ shift to new selection (rotate: end becomes begin)\n    for (int i = 0; i &lt; number_shifts - 1; i++)\n    {\n      it_players++;\n      if (it_players == list_players.end()) it_players = list_players.begin();\n    }\n    \/\/ move to team by inserting and deleting from list\n    team.insert(*it_players);\n    number_of_members_in_team++;\n    number_shifts = it_players-&gt;length(); \/\/ distribute algorithm: shift number of times as length of name of last chosen player\n    it_players = list_players.erase(it_players);\n    if (list_players.empty()) \/\/ just in case\n      return team;\n  }\n  return team;\n}\n\n\/\/ distribute players to given number of teams\nResultType Distribution(const std::vector&lt;std::string&gt;&amp;names, int number_teams) {\n  \/\/ init\n  ResultType teams(number_teams);\n  int number_players = names.size();\n  int number_of_first = number_players % number_teams;\n  int number_of_members = number_players \/ number_teams + 1; \/\/ adjusted after number_of_first\n  std::list&lt;std::string&gt; list_players(names.begin(), names.end());\n  auto it_players = list_players.begin();\n  \n  \/\/ do for all teams\n  for (int tm = 0; tm &lt; number_teams; ++tm) \n  {\n    if (tm == number_of_first) \/\/ adjust because not all teams can have the same size, so first teams can be larger by 1\n      number_of_members--;\n\n    teams[tm] = make_team(list_players, it_players, number_of_members);\n    if (list_players.empty())\n      return teams;\n  }\n  return teams;\n}\n\n\/\/ test harnass\nint main() {\n  for (auto i :\n       Distribution({\"Damir\", \"Ana\", \"Muhamed\", \"Marko\", \"Ivan\", \"Mirsad\",\n                     \"Nikolina\", \"Alen\", \"Jasmina\", \"Merima\"},\n                    3)) {\n    for (auto j : i)\n      std::cout &lt;&lt; j &lt;&lt; \" \";\n    std::cout &lt;&lt; std::endl;\n  }\n  return 0;\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Task with inserting elements to list &#8211; tough a little <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] One wrong thing in your check function: you only check the names in the current team to skip that, but you should skip any name that was already put in a team. I already gave you an alternative on another question, so the check function isn&#8217;t needed (erasing from the list). Also, when iterating &#8230; <a title=\"[Solved] Task with inserting elements to list &#8211; tough a little\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\" aria-label=\"More on [Solved] Task with inserting elements to list &#8211; tough a little\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,2757,4321],"class_list":["post-17388","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-stdlist","tag-stdset"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Task with inserting elements to list - tough a little - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Task with inserting elements to list - tough a little - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] One wrong thing in your check function: you only check the names in the current team to skip that, but you should skip any name that was already put in a team. I already gave you an alternative on another question, so the check function isn&#8217;t needed (erasing from the list). Also, when iterating ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-24T05:40:02+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Task with inserting elements to list &#8211; tough a little\",\"datePublished\":\"2022-10-24T05:40:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\"},\"wordCount\":241,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"stdlist\",\"stdset\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\",\"name\":\"[Solved] Task with inserting elements to list - tough a little - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-24T05:40:02+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Task with inserting elements to list &#8211; tough a little\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Task with inserting elements to list - tough a little - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Task with inserting elements to list - tough a little - JassWeb","og_description":"[ad_1] One wrong thing in your check function: you only check the names in the current team to skip that, but you should skip any name that was already put in a team. I already gave you an alternative on another question, so the check function isn&#8217;t needed (erasing from the list). Also, when iterating ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/","og_site_name":"JassWeb","article_published_time":"2022-10-24T05:40:02+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Task with inserting elements to list &#8211; tough a little","datePublished":"2022-10-24T05:40:02+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/"},"wordCount":241,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","stdlist","stdset"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/","url":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/","name":"[Solved] Task with inserting elements to list - tough a little - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-24T05:40:02+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-task-with-inserting-elements-to-list-tough-a-little\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Task with inserting elements to list &#8211; tough a little"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17388"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17388\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}