distance
Advanced Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: #include "stdafx.h" #include <vector> #include <algorithm> #include <functional> #include <numeric> #include <set> template <typename T, typename Container> struct Op : public std::unary_function<T, void> { Op(Container& c) : c_(c), value_(0), maxCnt_(0) {} result_type operator()(const argument_type& v) { size_t cnt = std::count(c_.begin(), c_.end(), v); if (cnt > maxCnt_) { maxCnt_ = cnt; value_ = v; } } T get_value() const { return (value_); } size_t maxCnt_; T value_; Container& c_; }; template <typename T> T find_most_recently_met(T* arr, size_t N) { typedef std::set<T> unique_type; typedef std::multiset<T> container_type; std::sort(arr, arr + N); unique_type res, u(arr, arr + N); container_type s(arr, arr + N); // выделяем только дублирующиеся элементы std::set_difference(s.begin(), s.end(), u.begin(), u.end(), std::inserter(res, res.begin())); // определяем кто из дублей наиболее часто встречается return (std::for_each(res.begin(), res.end(), Op<T, container_type>(s)).get_value()); } | Пример использования: Код: int arr[] = {...}; size_t N = sizeof(arr) / sizeof(arr[0]); int res = find_most_recently_met(arr, N); |
| Всего записей: 878 | Зарегистр. 28-03-2004 | Отправлено: 16:48 01-11-2007 | Исправлено: distance, 16:57 01-11-2007 |
|