std::multiset<int> set0, set1; for (auto it = set0.begin(); it != set0.end(); ++it) { // // // *it } for (auto it = set1.rbegin(); it != set1.rend(); ++it) { // // // *it }
size_t const N = 2; std::multiset<int> sets[N] = {/* ? */}; for (size_t i = 0; i < N; ++i) { for (auto const& val: sets[i]) { // // // val } }
std::multiset<int> sets[N] = {std::multiset<int, std::less>(), std::multiset<int, std::greater>()};
template <typename T> class Comparator { public: bool operator()(const T& l, const T& r) const { return ASC == order_ ? (l < r) : (l > r); } static Comparator const& Asc() { return asc_; } static Comparator const& Desc() { return desc_; } private: enum Order_ {ASC, DESC} const order_; Comparator(Order_ order) : order_(order) {}; static Comparator asc_; static Comparator desc_; }; template<typename T> Comparator<T> Comparator<T>::asc_(Comparator<T>::ASC); template<typename T> Comparator<T> Comparator<T>::desc_(Comparator<T>::DESC);
typedef std::multiset<int, Comparator<int> > Set; Set sets[N] = { Set(Comparator<int>::Asc()), Set(Comparator<int>::Desc()) };
template<typename T> bool compareLess(const T& l, const T& r) { return l < r; } template<typename T> bool compareGreater(const T& l, const T& r) { return l > r; }
typedef bool (*compareFn)(int const&, int const&); typedef std::multiset<int, compareFn> Set2; Set2 sets2[N] = { Set2(compareLess<int>), Set2(compareGreater<int>) };
#include <iostream> #include <set> #include <functional> using namespace std::placeholders; template <typename T> class Comparator { public: bool operator()(const T& l, const T& r) const { return ASC == order_ ? (l < r) : (l > r); } static Comparator const& Asc() { return asc_; } static Comparator const& Desc() { return desc_; } private: enum Order_ {ASC, DESC} const order_; Comparator(Order_ order) : order_(order) {}; static Comparator asc_; static Comparator desc_; }; template<typename T> Comparator<T> Comparator<T>::asc_(Comparator<T>::ASC); template<typename T> Comparator<T> Comparator<T>::desc_(Comparator<T>::DESC); template<typename T> bool compareLess(const T& l, const T& r) { return l < r; } template<typename T> bool compareGreater(const T& l, const T& r) { return l > r; } int main() { static size_t const N = 2; int unsorted[] = {4, 6, 3, 4, 7, 8, 1, 2}; //1 { typedef std::multiset<int, Comparator<int> > Set; Set sets[N] = { Set(Comparator<int>::Asc()), Set(Comparator<int>::Desc()) }; for (size_t i = 0; i < N; ++i) { sets[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets[i]) { std::cout << it << " "; } std::cout << "\n"; } } //2 { typedef bool (*compareFn)(int const&, int const&); typedef std::multiset<int, compareFn> Set2; Set2 sets2[N] = { Set2(compareLess<int>), Set2(compareGreater<int>) }; for (size_t i = 0; i < N; ++i) { sets2[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets2[i]) { std::cout << it << " "; } std::cout << "\n"; } } //3 { typedef std::multiset<int, std::function<bool(int,int)>> Set3; Set3 sets3[N] = {Set3([](int a, int b){ return a < b; }), Set3([](int a, int b){ return a > b; })}; for (size_t i = 0; i < N; ++i) { sets3[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets3[i]) { std::cout << it << " "; } std::cout << "\n"; } } //4 { typedef std::multiset<int, std::function<bool(int,int)>> Set4; Set4 sets4[N] = {Set4(std::less<int>()), Set4(std::bind(std::less<int>(), _2, _1))}; for (size_t i = 0; i < N; ++i) { sets4[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets4[i]) { std::cout << it << " "; } std::cout << "\n"; } } //5 { typedef std::multiset<int, std::function<bool(int,int)>> Set5; Set5 sets5[N] = {Set5(std::less<int>()), Set5(std::greater<int>())}; for (size_t i = 0; i < N; ++i) { sets5[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets5[i]) { std::cout << it << " "; } std::cout << "\n"; } } //6: 2 3 { typedef bool (*compareFn)(int const&, int const&); typedef std::multiset<int, compareFn> Set6; Set6 sets6[N] = {Set6([](int const& a, int const& b){ return a < b; }), Set6([](int const& a, int const& b){ return a > b; })}; for (size_t i = 0; i < N; ++i) { sets6[i].insert(std::begin(unsorted), std::end(unsorted)); } for (size_t i = 0; i < N; ++i) { for (auto const& it : sets6[i]) { std::cout << it << " "; } std::cout << "\n"; } } return 0; }
Source: https://habr.com/ru/post/210746/
All Articles