{"id":103279,"date":"2019-12-30T07:00:00","date_gmt":"2019-12-30T15:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103279"},"modified":"2019-12-29T20:35:41","modified_gmt":"2019-12-30T04:35:41","slug":"20191230-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191230-00\/?p=103279","title":{"rendered":"How do I make a clone of a Windows Runtime vector in C++\/CX?"},"content":{"rendered":"<p>There are still some people maintaining code bases written in C++\/CX, even though C++\/WinRT is the new hotness. Suppose you have a reference to a Windows Runtime vector in C++\/CX, either an <code>IVector&lt;T&gt;^<\/code> or an <code>IVector\u00adView&lt;T&gt;^<\/code>, and you want to clone it so that you can operate on the clone without affecting the original.<\/p>\n<p>You could create a vector and copy the items across:<\/p>\n<pre>IVector&lt;Thing^&gt;^ original = GetTheThings();\r\nVector&lt;Thing^&gt; vec = ref new Vector&lt;Thing^&gt;();\r\nfor (auto&amp;&amp; thing : original)\r\n{\r\n  vec-&gt;Append(thing);\r\n}\r\n<\/pre>\n<p>You can make the <code>Vector<\/code> run the loop by using the constructor overload that takes a pair of iterators.<\/p>\n<pre>IVector&lt;Thing^&gt;^ original = GetTheThings();\r\nVector&lt;Thing^&gt; vec = ref new Vector&lt;Thing^&gt;(begin(original), end(original));\r\n<\/pre>\n<p>Even more directly, you could slurp the entire collection into a <code>std::vector<\/code> and then move the <code>std::vector<\/code> into a new <code>Platform::<\/code><code>Collections::<\/code><code>Vector<\/code>.<\/p>\n<pre>IVector&lt;Thing^&gt;^ original = GetTheThings();\r\nVector&lt;Thing^&gt; vec = ref new Vector&lt;Thing^&gt;(to_vector(original));\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Taking shortcuts with special constructors.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Taking shortcuts with special constructors.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103279"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}