私は一般的な方法で group_by メソッドを実装して遊んでいましたが、おそらくそれを実装しました(C配列では機能しないことを除いて)が、それでもコードは私には醜く見えます...
私が望むことを行うためのより簡単な方法はありますか(+すべてのコンテナとC配列で機能するようにするには(C配列(TT)で機能させる方法がわかりません)?
それが明らかでない場合は、stdの型について話している::multimap... ところで、C++14 ではこれを 2 回入力する必要がなくなることはわかっています ( auto は、後で書き込むタイプを認識します -> )
// group_by.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <map>
#include <deque>
#include <cstdint>
#include <functional>
#include <algorithm>
#include <iostream>
template<typename Cont, typename F >
auto group_by (Cont c, F f) -> std::multimap< typename std::remove_reference<decltype(*std::begin(c))>::type, decltype(f(*std::begin(c)))>
{
std::multimap<typename std::remove_reference<decltype(*std::begin(c))>::type , decltype(f(*std::begin(c)))> result;
std::for_each(std::begin(c), std::end(c),
[&result,&f](typename Cont::value_type elem)
{
auto key = f(elem);
result.insert(std::make_pair(key,elem));
}
);
return result;
}
int main()
{
std::deque<uint64_t> dq;
std::deque<uint64_t>::value_type asghuitl;
dq.push_back(1);
dq.push_back(2);
dq.push_back(11);
dq.push_back(21);
auto result = group_by(dq, [] (uint64_t x){return x%10;});
}