趣味编程:静夜思(C++11版)

18 篇文章 0 订阅
10 篇文章 0 订阅
CppLinq的扩展
// cpplinqex.hpp
#include "cpplinq.hpp"
namespace cpplinq
{               
	namespace detail
	{
		template<typename TKeyPredicate, typename TSelector>
		struct to_groups_selector_builder : base_builder
		{
			typedef		to_groups_selector_builder<TKeyPredicate, TSelector>    this_type           ;
			typedef     TKeyPredicate                       key_predicate_type  ;
			typedef     TSelector							selector_type		;

			key_predicate_type          key_predicate   ;
			selector_type				selector   ;

			CPPLINQ_INLINEMETHOD explicit to_groups_selector_builder (
				key_predicate_type key_predicate
				, selector_type selector) throw ()
				:   key_predicate   (std::move (key_predicate))
				,   selector		(std::move (selector))
			{
			}

			CPPLINQ_INLINEMETHOD to_groups_selector_builder (to_groups_selector_builder const & v)
				:   key_predicate	(v.key_predicate)
				,   selector		(v.selector)
			{
			}

			CPPLINQ_INLINEMETHOD to_groups_selector_builder (to_groups_selector_builder && v) throw ()
				:   key_predicate	(std::move (v.key_predicate))
				,   selector		(std::move (v.selector))
			{
			}

			template<typename TRange>
			CPPLINQ_INLINEMETHOD 
				std::map<
					typename get_transformed_type<key_predicate_type, typename TRange::value_type>::type
					, std::vector<typename get_transformed_type<selector_type, typename TRange::value_type>::type>
				> build (TRange range)
			{
				std::map<
					typename get_transformed_type<key_predicate_type, typename TRange::value_type>::type
					, std::vector<typename get_transformed_type<selector_type, typename TRange::value_type>::type>
				> groupings;

				while (range.next ())
				{
					auto value  = range.front ();
					auto key    = key_predicate (value);
					groupings[key].push_back (selector (value));
				}

				return std::move (groupings);
			}
		};
	}

	template<typename TKeyPredicate, typename TSelector>
	CPPLINQ_INLINEMETHOD detail::to_groups_selector_builder<TKeyPredicate, TSelector>  to_groups (TKeyPredicate key_predicate, TSelector selector) throw ()
	{
		return detail::to_groups_selector_builder<TKeyPredicate, TSelector>(std::move (key_predicate), std::move (selector));
	}
}
静夜思
#include <iostream>
#include <clocale>
#include "cpplinqex.hpp"

using namespace std;
using namespace cpplinq;

void print(wstring const& text, int offset)
{
	auto groupings = from(text)
		>> zip_with(range(0, text.length()))
		>> to_groups([=](pair<wchar_t, int> const& p){return p.second % offset;},
			[](pair<wchar_t, int> const& p){return wstring(1, p.first);});
	for(auto const& kvs : groupings){
		auto v = from(kvs.second)
			>> reverse()
			>> concatenate(L"|");
		wcout << v << endl;
	}
}

int main()
{
	locale loc("chs");
	wcout.imbue(loc);
	print(L"床前明月光疑是地上霜举头望明月低头思故乡", 5);
}

//低|举|疑|床
//头|头|是|前
//思|望|地|明
//故|明|上|月
//乡|月|霜|光
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值