{"id":133,"date":"2009-07-24T12:02:02","date_gmt":"2009-07-24T11:02:02","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=133"},"modified":"2009-07-24T12:02:02","modified_gmt":"2009-07-24T11:02:02","slug":"vector","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/vector\/","title":{"rendered":"Vector"},"content":{"rendered":"<p>An vector is very similar to a array, in that it is able to store data within one variable name.  The main difference is that a vector is more of a class structure that allows you do to different tasks to it and also it acts similar to a stack FIFO (First In First Out) theory.<\/p>\n<p>Here is SGI vector functions (<a href=\"http:\/\/www.sgi.com\/tech\/stl\/Vector.html\" title=\"http:\/\/www.sgi.com\/tech\/stl\/Vector.html\" target=\"_blank\">SGI Vectors<\/a>)<\/p>\n<p>The vector allows for a dynamic approach of creating and inserting data onto a seemless endless array of data.  When a new element is placed onto the vector (push_back), this creates a new block of memory associated with the vector and the value is attached (you can only attached the value the same as the initial setup of the vector, an vector is able to use classes, variables etc to push\/pop of the vector\/stack).  <\/p>\n<p>This is the example code<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n#include <vector>\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n       \/\/ create a vector of intergers\r\n       vector<int> vectorInt;\r\n       \/\/ push_back, inserts onto the top of the list values\r\n       vectorInt.push_back(2);              \/\/ insert 2\r\n       vectorInt.push_back(3);              \/\/ insert 3\r\n       \r\n       \/\/ a iterator is basically a type of object that can reference part of the vector\r\n       vector<int>::iterator iteratorInt;\r\n       \r\n       \/\/ display all of the emplements inside the vector\r\n       for (iteratorInt = vectorInt.begin(); iteratorInt != vectorInt.end(); iteratorInt++)\r\n       {\r\n              cout << *iteratorInt << \"\\n\";              \/\/ display what the iterator pointers to \r\n       }\r\n       \/\/ display the size of a vector \r\n       cout << \"size = \" << vectorInt.size() << \"\\n\";\r\n       \r\n       \/\/ the capacity = how big the vector is, since there has been no pull\/remove\/erase from the vector\r\n       \/\/ then it is the same size.\r\n       cout << \"capacity = \" << vectorInt.capacity() << \"\\n\";\r\n       \r\n       cout << \"Last element \" << (int)vectorInt.back() << \"\\n\";              \/\/ the last element\r\n       vectorInt.pop_back();                            \/\/ delete the last element\r\n       cout << \"Last element \" << (int)vectorInt.back() << \"\\n\";              \/\/ the last element now\r\n                     \r\n       cout << \"capacity = \" << vectorInt.capacity() << \"\\n\";       \/\/ capacity is still 2\r\n       cout << \"size = \" << vectorInt.size() << \"\\n\";              \/\/ but the size is 1\r\n       \r\n       cout << \"max size = \" << vectorInt.max_size() << \"\\n\";       \r\n       return 0;\r\n}\r\n<\/pre>\n<p>if you save the code as vectorcpp.cpp, then compile and run, the output will be <\/p>\n<pre class=\"consoleoutput\">\r\n2\r\n3\r\nsize = 2\r\ncapacity = 2\r\nLast element 3\r\nLast element 2\r\ncapacity = 2\r\nsize = 1\r\nmax size = 1073741823\r\n<\/pre>\n<p>and as you can see the with removing the last element from the list (number 3), the size of the vector is reduced, but the capacity is the same since that is how much had been assigned to the vector.<\/p>\n<p>Note : the (int) in front of the vectorInt.back(), will decast the return value into a int(eger) value for the standard console output (std::cout).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An vector is very similar to a array, in that it is able to store data within one variable name. The main difference is that a vector is more of a class structure that allows you do to different tasks to it and also it acts similar to a stack FIFO (First In First Out) &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/07\/24\/vector\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Vector<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,9,8,398,400],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gcc","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":1,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":134,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions\/134"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}