{"id":109804,"date":"2024-05-24T07:00:00","date_gmt":"2024-05-24T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109804"},"modified":"2024-05-24T06:54:53","modified_gmt":"2024-05-24T13:54:53","slug":"20240524-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240524-00\/?p=109804","title":{"rendered":"Setting the contents of a Windows Runtime Vector from C++\/WinRT in one call"},"content":{"rendered":"<p>We saw earlier that you can build the initial contents of a Windows Runtime <code>IVector<\/code> in a <code>std::<wbr \/>vector<\/code> (which is usually far more convenient with better performance), and then <a title=\"If you have to create a Windows Runtime Vector from C++\/WinRT, do it as late as possible\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240522-00\/?p=109795\"> convert it to a Windows Runtime <code>IVector<\/code> as a final step<\/a>. Or you can <a title=\"Creating a prepopulated Windows Runtime Vector from C++\/WinRT without going through an explicit std::vector\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240523-00\/?p=109797\"> create the Windows Runtime <code>IVector<\/code> from raw materials without having to use an explicit <code>std::<wbr \/>vector<\/code> at all<\/a>.<\/p>\n<p>But what if somebody gave you an existing Windows Runtime <code>IVector<\/code>, and you want to overwrite its current contents with new content? This happens in many parts of the Windows Runtime, such as the <code>File\u00adOpen\u00adPicker<\/code>, which gives you a <code>File\u00adType\u00adFilter<\/code> that you can fill with the file types you want to filter for. You can&#8217;t provide your own <code>IVector<\/code>; you have to fill the existing one.<\/p>\n<p>The na\u00efve way would be to clear the vector and then fill it with items, one at a time:<\/p>\n<pre>namespace winrt\r\n{\r\n    using namespace winrt::Windows::Storage::Pickers;\r\n}\r\n\r\nwinrt::FileOpenPicker CreatePickerForSupportedImages()\r\n{\r\n    winrt::FileOpenPicker picker;\r\n    auto filter = picker.FileTypeFilter();\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">filter.Clear();        <\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">filter.Append(L\".jpg\");<\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">filter.Append(L\".png\");<\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">filter.Append(L\".bmp\");<\/span>\r\n    <span style=\"border: 1px currentcolor; border-style: none solid;\">filter.Append(L\".gif\");<\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">filter.Append(L\".tif\");<\/span>\r\n    return picker;\r\n}\r\n<\/pre>\n<p>But there&#8217;s a one-stop way of doing this: The <code>Replace\u00adAll<\/code> method.<\/p>\n<pre>winrt::FileOpenPicker CreatePickerForSupportedImages()\r\n{\r\n    winrt::FileOpenPicker picker;\r\n    auto filter = picker.FileTypeFilter();\r\n    <span style=\"border: solid 1px currentcolor;\">filter.ReplaceAll({ L\".jpg\", L\".png\", L\".bmp\", L\".gif\", L\".tif\" });<\/span>\r\n    return picker;\r\n}\r\n<\/pre>\n<p>The <code>ReplaceAll<\/code> method replaces the entire contents of the vector with the values you provide. You can think of it as a combination <code>Clear<\/code> and <code>Append<\/code>, but it all happens at once.<\/p>\n<p>The <code>ReplaceAll<\/code> method takes a <code>winrt::<wbr \/>array_view<\/code>, so you can pass anything that an <code>array_view<\/code> can construct from. In this example, we used an <code>initializer_list<\/code>, but you can <a title=\"winrt::array_view struct template (C++\/WinRT)\" href=\"https:\/\/learn.microsoft.com\/uwp\/cpp-ref-for-winrt\/array-view\"> look at the other constructors<\/a> to see all the options you have available.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The one-stop shop for updating a Windows Runtime Vector.<\/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-109804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The one-stop shop for updating a Windows Runtime Vector.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109804","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=109804"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109804\/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=109804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}