{"id":3973,"date":"2014-03-30T06:45:29","date_gmt":"2014-03-30T06:45:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-copy-a-char-pointer-array-into-a-vector-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:45:29","modified_gmt":"2014-03-30T06:45:29","slug":"how-to-copy-a-char-pointer-array-into-a-vector-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-copy-a-char-pointer-array-into-a-vector-in-c-collection-of-common-programming-errors\/","title":{"rendered":"how to copy a char pointer array into a vector in c++ ?-Collection of common programming errors"},"content":{"rendered":"<p>Hello Priya,<\/p>\n<p>actually I would not recommend to use vector::push_back(). It will cause several reallocations which can be avoided if you know the size of the original structure. Instead I would recommend the constructor overload of vector which takes two iterators. If the vector already exists vector::insert will do fine also. Here is my suggestion:<\/p>\n<pre>\/\/copying from a C char* array\nchar** var_name = new varName[100];\n\n\/\/copies elements 0 to 99\nvector myVector ( &amp;var_name[0], &amp;var_name[100] );\n\n\/\/coping from a standard container\nset haplotype;\n\/\/fill set with some data...\n\nvector myVec;\nmyVec.reserve( haplotype.size() );\nmyVec.insert( haplotype.begin(), haplotype.end() );<\/pre>\n<p>best regards,<br \/>\nMarkus Klein<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello Priya, actually I would not recommend to use vector::push_back(). It will cause several reallocations which can be avoided if you know the size of the original structure. Instead I would recommend the constructor overload of vector which takes two iterators. If the vector already exists vector::insert will do fine also. Here is my suggestion: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3973","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3973","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3973"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3973\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}