zwvista

导航

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

#include <iostream>
#include <range/v3/all.hpp>
#include <vector>
#include <locale>
#include <locale.h>
using namespace std;
using namespace ranges;

int main(int argc, const char * argv[]) {
    constexpr char locale_name[] = "en_US.UTF-8";
    setlocale( LC_ALL, locale_name );
    locale::global(locale(locale_name));
    wcout.imbue(locale());
    wstring text = L"床前明月光疑是地上霜举头望明月低头思故乡";
    int offset = 5;
    auto v = view::iota(0, text.size()) | to_vector;
    auto v2 = action::sort(v, [&](int a, int b){return pair{a % offset, -a} < pair{b % offset, -b};}) |
        view::group_by([&](int a, int b){return a % offset == b % offset;}) | to_vector;
    auto rng = v2 | view::transform([&](auto& o){
        return o | view::transform([&](int i){return text[i];}) | view::intersperse(L'|');
    });
    for (wstring o : rng)
        wcout << o << endl;
    return 0;
}

/*
低|举|疑|床
头|头|是|前
思|望|地|明
故|明|上|月
乡|月|霜|光
*/

posted on 2019-06-07 18:28  zwvista  阅读(741)  评论(0编辑  收藏  举报