{"id":2423,"date":"2016-01-15T12:07:00","date_gmt":"2016-01-15T12:07:00","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2423"},"modified":"2020-03-29T13:12:32","modified_gmt":"2020-03-29T13:12:32","slug":"vectors-general","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/memory\/vectors\/vectors-general","title":{"rendered":".Vectors General"},"content":{"rendered":"\n<p>Vectors are sequence containers representing arrays that can change in size. &nbsp;They&nbsp;use contiguous storage locations for their elements so can be accessed in the same way and as efficiently as arrays,&nbsp;but unlike arrays&nbsp;their size can change dynamically, with their storage being handled automatically by the container.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Create a vector<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n#include &lt;vector>\n\n\tvector&lt;string> MyVector1;\n\n\tMyVector1.push_back(\"Hello\");\t\t\t\/\/push_back = Add a new item to the end\n\tMyVector1.push_back(\"World\");\n\n\tint Index;\t\n\tfor (Index = 0; Index &lt; MyVector1.size(); Index++)\n\t\tstd::cout &lt;&lt; \"Next vector element: \" &lt;&lt; MyVector1[Index] &lt;&lt; std::endl;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Length of a vector<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t = MyVector1.size();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete item at index<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyVector1.erase(MyVector1.begin()+2);\t\t\/\/Delete vector entry at index 2<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete all items<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyVector1.clear();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Insert item at index<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tMyVector1.insert((MyVector1.begin() + 0), \"Hello\");\t\t\/\/UInsert new item at index 0<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vectors are sequence containers representing arrays that can change in size. &nbsp;They&nbsp;use contiguous storage locations for their elements so can be accessed in the same way and as efficiently as arrays,&nbsp;but unlike arrays&nbsp;their size can change dynamically, with their storage being handled automatically by the container. Create a vector Length of a vector Delete item [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-2423","post","type-post","status-publish","format-standard","hentry","category-vectors"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2423"}],"version-history":[{"count":8,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2423\/revisions"}],"predecessor-version":[{"id":3184,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2423\/revisions\/3184"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}