diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index eae16f41..4e761ac3 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -1,7 +1,9 @@ #pragma once +#include #include #include +#include #include #include @@ -67,6 +69,32 @@ class Helper return data[index]; } + /** + * @brief Get a random element from a std::set. + * + * @tparam T an element type of the std::set. + * + * @param std::set of elements. + * + * @return T a random element from the std::set. + * + * @code + * std::set chars{'a', 'b', 'c', 'd', 'e'}; + * Helper::setElement(chars) // 'd' + * @endcode + */ + template + static T setElement(const std::set& data) + { + if (data.empty()) + { + throw std::invalid_argument{"Data is empty."}; + } + T item; + std::sample(data.begin(), data.end(), &item, 1, pseudoRandomGenerator); + return item; + } + template struct WeightedElement {