趣味编程:静夜思(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;
}

/*
低|举|疑|床
头|头|是|前
思|望|地|明
故|明|上|月
乡|月|霜|光
*/
原文地址:https://www.cnblogs.com/zwvista/p/10988870.html