1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <tuple>
#include <array>
#include <map>
#include <list>
template< typename Tuple , typename Functor , size_t index = 0 >
void foreach( const Tuple& tpl , const Functor& f )
{
constexpr auto tuple_size = std::tuple_size_v<Tuple>;
if constexpr( index < tuple_size )
{
f( std::get<index>(tpl) );
foreach< Tuple , Functor , index+1 >(tpl, f);
}
}
int main()
{
std::map< std::tuple<int,int,int,int> , std::array<char,3> > mp;
mp.insert({{1,0,0,0},{'a','b','c'}});
mp.insert({{1,1,0,0},{'d','e','f'}});
for( const auto& [key,value] : mp )
{
foreach(key,[](const auto& v){ std::cout << v; }); std::cout << " ";
for( char c : value ){ std::cout << c; } std::cout << std::endl;
}
}
|