百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
S

Streams

> 编程语言
开源

C++ 中的懒惰求值 - http://jscheiny.GitHub.io/Streams/

688 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

C++ 中的懒惰求值 - http://jscheiny.GitHub.io/Streams/

C++ Streams

Streams is a C++ library that provides lazy evaluation and functional-style transformations on the data, to ease the use of C++ standard library containers and algorithms. Streams support many common functional operations such as map, filter, and reduce, as well as various other useful operations such as various set operations (union, intersection, difference), partial sum, and adjacent difference, as well as many others.

To use, simply #include "Stream.h", and compile using a C++14 compatible compiler. All streams classes/functions can be found in the stream namespace.

Links:

  • Documentation
  • Github
  • Contact

C++ Streams are distributed under the MIT open source license. Copyright (c) 2014 by Jonah Scheinerman

Examples

Coin flip experiment:

cpp
using namespace stream;
using namespace stream::op;

int number_heads(int flips) {
    return MakeStream::coin_flips()
        | limit(flips)
        | filter()
        | count();
};

void experiment(int trials, int flips) {
    auto stats = MakeStream::generate(std::bind(number_heads, flips))
        | limit(trials)
        | reducers::SummaryStats().reducer();
    std::cout  ");
    std::cout  12 -> 6 -> 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

Vector operations:

cpp
std::vector x = /* ... */;
std::vector y = /* ... */;

auto to_stream = [](std::vector& vec) {
    return MakeStream::from(vec);
};

std::vector sum_vec = to_stream(x) + to_stream(y);
std::vector diff_vec = to_stream(x) - to_stream(y);
double dot_product = (to_stream(x) * to_stream(y)) | sum();
std::vector scaling = to_stream(x) * 10;
std::vector translating = to_stream(x) + 3.7;

Set operations:

cpp
std::set x = /* ... */;
set::set y = /* ... */;

auto to_stream = [](std::set& vec) {
    return MakeStream::from(vec);
};

std::set set_union = to_stream(x) | union_with(to_stream(y));
// Better than:
//   std::set result;
//   std::set_union(x.begin(), x.end(), y.begin(), y.end(),
//                  inserter(result, result.end()));
std::set set_intersect = to_stream(x)
    | intersection_with(to_stream(y));
std::set set_diff = to_stream(x)
    | difference_with(to_stream(y));
std::set set_sym_diff = to_stream(x)
    | symmetric_difference_with(to_stream(y));

Adding unique ids:

cpp
std::vector objects = /* ... */;

std::vector objects_with_ids = MakeStream::from(objects)
    | zip_with(MakeStream::counter(1), [](T&& object, int id) {
        object.set_id(id);
        return object;
    });

Printing containers:

cpp
(MakeStream::from(container) | print_to(std::cout)) (tup) % n == 0; })
         | map_([](auto&& tup) { return std::get(tup); });
};

MakeStream::from({1, 3, 8, 4, 7}) | every_nth(2) | print_to(std::cout); // 1 8 7

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言