{"id":7887,"date":"2022-09-10T20:10:20","date_gmt":"2022-09-10T14:40:20","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/"},"modified":"2022-09-10T20:10:20","modified_gmt":"2022-09-10T14:40:20","slug":"solved-vector-push_back-add-new-items","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/","title":{"rendered":"[Solved] vector push_back add new items"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-72589063\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"72589063\" data-parentid=\"72588971\" data-score=\"4\" 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>You&#8217;re pushing five pointers to the same array into your vector, so when you print the contents of the array pointed to by each pointer they&#8217;re all the same.<\/p>\n<p>You only have a single array: <code>txt<\/code>.  Each time through your loop, you write new contents into that array and push a pointer to it into <code>Lista<\/code>.  So the first time through your loop you have this:<\/p>\n<pre><code>txt\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502     \u2502     \u2502     \u2502     \u2502      \u2502     \u2502     \u2502     \u2502     \u2502     \u2502\n\u2502 'n' \u2502 'u' \u2502 'm' \u2502 '0' \u2502 '\\0' \u2502  ?  \u2502  ?  \u2502  ?  \u2502  ?  \u2502  ?  \u2502\n\u2502     \u2502     \u2502     \u2502     \u2502      \u2502     \u2502     \u2502     \u2502     \u2502     \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n\u25b2\n\u2502\n\u2514\u2500\u2500\u2500\u2510\n    \u2502\n\u250c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2510\n\u2502   \u2502   \u2502\n\u2502   \u2502   \u2502\n\u2502       \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\nLista\n<\/code><\/pre>\n<p>Then the second time through you modify <code>txt<\/code> and add another pointer to it to <code>Lista<\/code>, so you have this:<\/p>\n<pre><code>txt\n\u250c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502     \u2502     \u2502     \u2502     \u2502      \u2502     \u2502     \u2502     \u2502     \u2502     \u2502\n\u2502 'n' \u2502 'u' \u2502 'm' \u2502 '1' \u2502 '\\0' \u2502  ?  \u2502  ?  \u2502  ?  \u2502  ?  \u2502  ?  \u2502\n\u2502     \u2502     \u2502     \u2502     \u2502      \u2502     \u2502     \u2502     \u2502     \u2502     \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\n\u25b2 \u25b2\n\u2502 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2514\u2500\u2500\u2500\u2510       \u2502\n    \u2502       \u2502\n\u250c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u253c\u2500\u2500\u2500\u2510\n\u2502   \u2502   \u2502   \u2502   \u2502\n\u2502   \u2502   \u2502   \u2502   \u2502\n\u2502       \u2502       \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\nLista\n<\/code><\/pre>\n<p>And so on.  Every element of <code>Lista<\/code> contains a pointer to the same array, which you modify in each iteration of your loop.  When you go back and print the contents of the array pointed to by each element of <code>Lista<\/code>, they all point to the same array, so the same thing gets printed for each.<\/p>\n<p>If you want to store different text in each element of <code>Lista<\/code>, you will need to create a separate string for each.  The easiest way to do that would be to change the type of <code>Lista<\/code> to <code>std::vector&lt;std::string&gt;<\/code> and let the <code>std::string<\/code> class handle allocating space for each string:<\/p>\n<pre><code>for (int x = 0; x &lt;= 5; ++x) {\n    std::string text = std::format(num{}, x);  \/\/ Use std::ostringstream or sprintf if your compiler doesn't support std::format\n    Lista.push_back(text);\n}\n<\/code><\/pre>\n<p>Then you can use <code>std::string<\/code>&#8216;s <code>data<\/code> member function to get a pointer to the string&#8217;s underlying <code>char*<\/code> if you need to pass it to some interface that doesn&#8217;t support <code>std::string<\/code>.  Keep in mind that the lifetime of the arrays pointed to by those pointers are tied to the lifetime of the <code>std::string<\/code> object, so be careful not to use them after the <code>std::string<\/code> (or the <code>std::vector<\/code> that contains it) goes out of scope.<\/p>\n<p>If you absolutely do not want to use <code>std::string<\/code> to manage the lifetime of your arrays, you can allocate them yourself with <code>new[]<\/code>, just remember that you must then remember to <code>delete[]<\/code> them when you&#8217;re done with them to avoid leaking memory:<\/p>\n<pre><code>for (int x = 0; x &lt;= 5; ++x) {\n    char* txt = new char[5];\n    sprintf(txt, \"num%d\", x);\n    Lista.push_back(txt);\n}\n\n\/\/ ... stuff\n\nfor (char* txt : Lista) {\n    delete[] txt;\n}\n<\/code><\/pre>\n<p>There are very few legitimate reasons to do this though.  In 99.9% of cases you should use <code>std::string<\/code> or some sort of smart pointer to manage your memory.<\/p>\n<hr>\n<p>Note: Your program also exhibits undefined behavior because you access your array out of bounds with <code>Lista[x+1] = txt<\/code>; you should get rid of that line.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved vector push_back add new items <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You&#8217;re pushing five pointers to the same array into your vector, so when you print the contents of the array pointed to by each pointer they&#8217;re all the same. You only have a single array: txt. Each time through your loop, you write new contents into that array and push a pointer to it &#8230; <a title=\"[Solved] vector push_back add new items\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\" aria-label=\"More on [Solved] vector push_back add new items\">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],"class_list":["post-7887","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] vector push_back add new items - 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-vector-push_back-add-new-items\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] vector push_back add new items - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You&#8217;re pushing five pointers to the same array into your vector, so when you print the contents of the array pointed to by each pointer they&#8217;re all the same. You only have a single array: txt. Each time through your loop, you write new contents into that array and push a pointer to it ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-10T14:40:20+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] vector push_back add new items\",\"datePublished\":\"2022-09-10T14:40:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\"},\"wordCount\":357,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\",\"name\":\"[Solved] vector push_back add new items - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-10T14:40:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] vector push_back add new items\"}]},{\"@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] vector push_back add new items - 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-vector-push_back-add-new-items\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] vector push_back add new items - JassWeb","og_description":"[ad_1] You&#8217;re pushing five pointers to the same array into your vector, so when you print the contents of the array pointed to by each pointer they&#8217;re all the same. You only have a single array: txt. Each time through your loop, you write new contents into that array and push a pointer to it ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/","og_site_name":"JassWeb","article_published_time":"2022-09-10T14:40:20+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] vector push_back add new items","datePublished":"2022-09-10T14:40:20+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/"},"wordCount":357,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/","url":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/","name":"[Solved] vector push_back add new items - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-10T14:40:20+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-vector-push_back-add-new-items\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] vector push_back add new items"}]},{"@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\/7887","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=7887"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7887\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}