diff --git a/include/wx/arrstr.h b/include/wx/arrstr.h index f7c70c20f0..61b833b8a2 100644 --- a/include/wx/arrstr.h +++ b/include/wx/arrstr.h @@ -79,6 +79,10 @@ public: wxArrayString(size_t sz, const wxString* a); template wxArrayString(std::initializer_list list) : wxBaseArray(list) { } + wxArrayString(const std::vector& vec) : wxBaseArray(vec) { } + wxArrayString(std::vector&& vec) : wxBaseArray(std::move(vec)) { } + template + wxArrayString(const std::vector& vec) : wxBaseArray(vec.begin(), vec.end()) { } int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const; @@ -183,6 +187,9 @@ public: // list constructor template wxArrayString(std::initializer_list list) { Init(false); assign(list.begin(), list.end()); } + // ctor from a std::vector + template + wxArrayString(const std::vector& vec) { Init(false); assign(vec.begin(), vec.end()); } // assignment operator wxArrayString& operator=(const wxArrayString& src); // not virtual, this class should not be derived from diff --git a/include/wx/dynarray.h b/include/wx/dynarray.h index 02cc4b2faf..226f17dc07 100644 --- a/include/wx/dynarray.h +++ b/include/wx/dynarray.h @@ -110,6 +110,9 @@ public: template wxBaseArray(std::initializer_list list) : base_vec(list.begin(), list.end()) {} + wxBaseArray(const std::vector& vec) : base_vec(vec) { } + wxBaseArray(std::vector&& vec) : base_vec(std::move(vec)) { } + void Empty() { this->clear(); } void Clear() { this->clear(); } void Alloc(size_t uiSize) { this->reserve(uiSize); } diff --git a/interface/wx/arrstr.h b/interface/wx/arrstr.h index ec7d80c7f9..01b0d3a0bb 100644 --- a/interface/wx/arrstr.h +++ b/interface/wx/arrstr.h @@ -97,6 +97,30 @@ public: template wxArrayString(std::initializer_list list); + /** + Constructs the container with the contents of the vector @a vec. + + Template parameter `T` must be convertible to wxString, i.e. it can be + wxString itself or `std::string`, `std::wstring` etc. + + @since 3.3.0 + */ + template + wxArrayString(const std::vector& vec); + + /** + Constructs the container with the contents of the vector @a vec. + + When using @ref overview_container_std, this constructor is more + efficient than the overload taking const reference to the vector. + Otherwise it is identical to the other overload, see its documentation + for more details. + + @since 3.3.0 + */ + template + wxArrayString(std::vector&& vec); + /** Destructor frees memory occupied by the array strings. For performance reasons it is not virtual, so this class should not be derived from. diff --git a/tests/arrays/arrays.cpp b/tests/arrays/arrays.cpp index c5547b6747..15dce0b0bc 100644 --- a/tests/arrays/arrays.cpp +++ b/tests/arrays/arrays.cpp @@ -355,6 +355,25 @@ TEST_CASE("wxArrayString", "[dynarray]") CHECK( a9.size() == 5 ); } +TEST_CASE("wxArrayString::FromVector", "[dynarray][vector]") +{ + SECTION("wxString") + { + std::vector vec{"first", "second"}; + wxArrayString a(vec); + REQUIRE( a.size() == 2 ); + CHECK( a[1] == "second" ); + } + + SECTION("string") + { + std::vector vec{"third", "fourth"}; + wxArrayString a(vec); + REQUIRE( a.size() == 2 ); + CHECK( a[1] == "fourth" ); + } +} + TEST_CASE("wxSortedArrayString", "[dynarray]") { wxSortedArrayString a;