{"id":7345,"date":"2014-04-21T05:08:21","date_gmt":"2014-04-20T19:08:21","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7345"},"modified":"2014-04-21T05:08:21","modified_gmt":"2014-04-20T19:08:21","slug":"c-stdvector-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-stdvector-primer-tutorial\/","title":{"rendered":"C++ std::vector Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/Std_Vector.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"C++ std::vector Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/Std_Vector.jpg\" title=\"C++ std::vector Primer Tutorial\"   \/><\/a><p class=\"wp-caption-text\">C++ std::vector Primer Tutorial<\/p><\/div>\n<p>Today we answer a Yahoo Answers <a target=_blank title='Find prime numbers between a given pair of numbers and store them into an array? ' href='https:\/\/au.answers.yahoo.com\/question\/index?qid=20140418111413AAAFWcO'>question<\/a> (thanks) to point out an advantage C++ std::vector &#8220;container&#8221; datatype has over a base array datatype.  We use Xcode (on a Mac laptop) as our IDE for this work.<\/p>\n<p>Read more <a target=_blank title='std::vector' href='http:\/\/www.cplusplus.com\/reference\/vector\/vector\/'>here<\/a> about std::vector but let&#8217;s quote from that same useful link (thanks) where we define std::vector as &#8220;sequence containers representing arrays that can change in size&#8221;.   And that is the essence of today&#8217;s first bit of code that reads a very simple text file containing two integer numbers of ranges of numbers between which we find a list of prime numbers.<\/p>\n<p>Are you into syntax?  Does &#8220;<i>template < class T, class Alloc = allocator<t> > class vector; <\/i>&#8221; (via that same previous link) turn you on?  Hmmmmmmm.  Seriously though, syntax is good to learn well, so that you don&#8217;t have to look back at books (because, rest assured, they&#8217;re looking back at you with a kind of dumb stupid look) or other reference stuff all the time.<\/p>\n<p>So we have a progression &#8230; and regression &#8230; of code below:<\/p>\n<ul>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/main.cpp_GETME\">main.cpp<\/a> uses std::vector container with dynamic container resizing (using <a target=_blank title='std::vector resize() method' href='http:\/\/www.cplusplus.com\/reference\/vector\/vector\/resize\/'>resize()<\/a> method)<\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/main.cpp-GETME\">main.cpp<\/a> uses std::vector container with dynamic container resizing (and using <a target=_blank title='std::vector resize() method' href='http:\/\/www.cplusplus.com\/reference\/vector\/vector\/resize\/'>resize()<\/a> and <a target=_blank title='std::vector size() method' href='http:\/\/www.cplusplus.com\/reference\/vector\/vector\/size\/'>size()<\/a> and <a target=_blank title='std::vector push_back() method' href='http:\/\/www.cplusplus.com\/reference\/vector\/vector\/push_back\/'>push_back()<\/a> methods)<\/li>\n<li><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/main.cppGETME\">main.cpp<\/a> reverts back to array usage by declaring the array after a first pass of just getting the array size requirement &#8230; this doesn&#8217;t appear too much more complicated than the way with std::vector but imagine if you had more work to do with the records of the file as you read them (and then you&#8217;d have had to reread the file, which is awkward) &#8230; (C has <a target=_blank title='C malloc, realloc, calloc' href='http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=6728'>malloc and realloc and calloc<\/a> (and C++ probably can as well)) but the &#8220;spirit&#8221; of C++ is to use std::vector (methinks)<\/li>\n<\/ul>\n<p>Any good programming language will have several ways to kill simple cats (so feed your cat fishoil to avoid this) and another approach would be to use a List.  Read about some of the debate regarding Lists versus Vectors <a target=_blank title='Lists vs Vectors' href='http:\/\/programmers.stackexchange.com\/questions\/185222\/what-is-the-point-of-using-lists-over-vectors-in-c'>here<\/a>.<\/p>\n<p>Enjoy today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/CPlusPlus\/vector\/Std_Vector.jpg\" title='Click picture.'>tutorial<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7345' onclick='var dv=document.getElementById(\"d7345\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Javascript\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7345' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Today we answer a Yahoo Answers question (thanks) to point out an advantage C++ std::vector &#8220;container&#8221; datatype has over a base array datatype. We use Xcode (on a Mac laptop) as our IDE for this work. Read more here about &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/c-stdvector-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":[12,37,40],"tags":[176,710,997,1319,1366,1473,1488],"class_list":["post-7345","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","category-xcode","tag-c","tag-list","tag-programming","tag-tutorial","tag-vector","tag-xcode","tag-yahoo-answers"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7345"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=7345"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7345\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=7345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=7345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=7345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}