diff --git a/include/wx/vector.h b/include/wx/vector.h index c8b6320653..7b10147b6c 100644 --- a/include/wx/vector.h +++ b/include/wx/vector.h @@ -26,6 +26,12 @@ inline void wxVectorSort(wxVector& v) std::sort(v.begin(), v.end()); } +template +inline bool wxVectorContains(const wxVector& v, const T& obj) +{ + return std::find(v.begin(), v.end(), obj) != v.end(); +} + #else // !wxUSE_STD_CONTAINERS #include "wx/scopeguard.h" @@ -688,7 +694,17 @@ void wxVectorSort(wxVector& v) wxPrivate::wxVectorComparator::Compare, NULL); } +template +inline bool wxVectorContains(const wxVector& v, const T& obj) +{ + for ( size_t n = 0; n < v.size(); ++n ) + { + if ( v[n] == obj ) + return true; + } + return false; +} #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS diff --git a/interface/wx/vector.h b/interface/wx/vector.h index 60d953d94b..1b7ba19326 100644 --- a/interface/wx/vector.h +++ b/interface/wx/vector.h @@ -307,3 +307,13 @@ public: */ template void wxVectorSort(wxVector& v); + +/** + Returns true if the vector contains the given value. + + This is just a trivial wrapper around std::find(). + + @since 3.1.5 + */ +template +bool wxVectorContains(const wxVector& v, const T& value); diff --git a/tests/vectors/vectors.cpp b/tests/vectors/vectors.cpp index 0081f56450..166133718f 100644 --- a/tests/vectors/vectors.cpp +++ b/tests/vectors/vectors.cpp @@ -288,6 +288,22 @@ TEST_CASE("wxVector::Sort", "[vector][sort]") } } +TEST_CASE("wxVector::Contains", "[vector][contains]") +{ + wxVector v; + CHECK( !wxVectorContains(v, 0) ); + + v.push_back(3); + CHECK( wxVectorContains(v, 3) ); + + v.push_back(2); + v.push_back(3); + + CHECK( wxVectorContains(v, 2) ); + CHECK( wxVectorContains(v, 3) ); + CHECK( !wxVectorContains(v, 1) ); +} + TEST_CASE("wxVector::operator==", "[vector][compare]") { wxVector v1, v2;