{"id":295,"date":"2010-07-06T08:11:17","date_gmt":"2010-07-06T08:11:17","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=295"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"arrays-vectors","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/memory\/arrays\/arrays-vectors","title":{"rendered":".Arrays (Vectors)"},"content":{"rendered":"<p>\nModern C++\/CLI programs should almost always use vectors and iteriators in preference to the lower-level arrays and pointers, and strings to replace C-style array based character strings. Well designed programs use arrays and pointers only in the internals of class implementations where speed is essential. As well as being less powerful, C-style strings are the root cause of many many security problems. A vector is a collection of objects of a single type, each of which has an associated integer index. Its much the same as an array but unlike arrays it allows new elements to be added at run time.\n<\/p>\n<h4>\nList or Array?<br \/>\n<\/h4>\n<p>\nIf your going to be resizing your array as you add and remove items often using&nbsp;a list is best (resizing an array causes it to be copied). &nbsp;If you need to be able to access array elements by index (rather than using for each) then use an array. &nbsp;If you need both then you can use a list to create the final set of objects and then convert it to an array, or just use an array.\n<\/p>\n<h4>\nCreate Arrays<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tarray&lt;Byte&gt; ^MyArray;\t\t\t\t\/\/Declare a handle first\r\n\tMyArray = gcnew array&lt;Byte&gt;(3);\t\t\/\/Create the array\r\n<\/code><\/pre>\n<p>\nor use this to declare it:\n<\/p>\n<pre>\r\n<code>\r\n\tarray&lt;Byte&gt; ^MyArray = gcnew array&lt;Byte&gt;(3);\t\t\/\/Declare and allocate space for the array\r\n<\/code><\/pre>\n<p>\nor this to set values at creation\n<\/p>\n<pre>\r\n<code>\r\n\tarray&lt;Byte&gt; ^yValues = {10, 27.5, 7, 12, 45.5};\r\n<\/code><\/pre>\n<h4>\nMulti Dimension Arrays<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tarray&lt;String^, 2&gt; ^MyMultiDimensionArray = gcnew array&lt;String^, 2&gt;(3, 10);\r\n\tMyMultiDimensionArray[0,0] = &quot;A0&quot;;\r\n\tMyMultiDimensionArray[0,5] = &quot;B0&quot;;\r\n\tMyMultiDimensionArray[1,0] = &quot;A1&quot;;\r\n\tMyMultiDimensionArray[1,5] = &quot;B1&quot;;\r\n\tMyMultiDimensionArray[2,0] = &quot;A2&quot;;\r\n\tMyMultiDimensionArray[2,5] = &quot;B2&quot;;\r\n<\/code><\/pre>\n<h4>\nAccessing Arrays Items<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tpos[0] = 1;\t\t\t\t\t\t\/\/How to use the array\r\n\tpos[2] = 5;\r\n<\/code><\/pre>\n<h4>\nFor Each<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tarray&lt;Byte&gt; ^values = {3, 5, 20, 14};\r\n\tfor each (int item in values)\r\n\t{\r\n\t\titem = 0;\r\n\t}\r\n\tor\r\n\tarray&lt;String^&gt; ^names = {&quot;Bill&quot;, &quot;Jane&quot;, &quot;Ted&quot;};\r\n\tfor each (String^ name in names)\r\n\t{\r\n\t\tname = name + &quot;\\n&quot;;\r\n\t}\r\n<\/code><\/pre>\n<h4>\nCalling A Function With An Array<br \/>\n<\/h4>\n<p>\nHave this in the function parameters:\n<\/p>\n<pre>\r\n<code>\tvoid some_function (array&lt;Byte&gt; ^distanceData)<\/code><\/pre>\n<p>\nAnd call like this\n<\/p>\n<pre>\r\n<code>\tsome_function(values);\t\t\/\/No &#39;^&#39; character - it knows its a handle<\/code><\/pre>\n<h4>\nPass An Array Handle To Use By Some Other Function In A Class<br \/>\n<\/h4>\n<p>\nDefine the array handle in the class definition:\n<\/p>\n<pre>\r\n<code>\tarray&lt;Byte&gt; ^MyArrayName;\t\t\t\/\/Define the handle but don&#39;t create the actual array<\/code><\/pre>\n<p>\nThen within some function you can just use:\n<\/p>\n<pre>\r\n<code>\tMyArrayName = AnotherArrayName;\t\t\/\/Assign the handle of another array - you read and write the other array not a copy<\/code><\/pre>\n<h4>\nClass Public Array<br \/>\n<\/h4>\n<p>\nIn the public variable section:\n<\/p>\n<pre>\r\n<code>\tarray&lt;Byte&gt; ^MyArrayName;<\/code><\/pre>\n<p>\nIn the constructor:\n<\/p>\n<pre>\r\n<code>\tMyArrayName = gcnew array&lt;Byte&gt;(6);<\/code><\/pre>\n<h4>\nArray Length<br \/>\n<\/h4>\n<pre>\r\n<code>\tMyArrayName-&gt;Length<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Modern C++\/CLI programs should almost always use vectors and iteriators in preference to the lower-level arrays and pointers, and strings to replace C-style array based character strings. Well designed programs use arrays and pointers only in the internals of class implementations where speed is essential. As well as being less powerful, C-style strings are the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-295","post","type-post","status-publish","format-standard","hentry","category-arrays"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=295"}],"version-history":[{"count":13,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":1483,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/295\/revisions\/1483"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}