Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

Streams

> 编程语言
Open source

Lazy evaluation in C++ - http://jscheiny.github.io/Streams/

688 stars0 likes0 views
WebsiteGitHub

About

Lazy evaluation in 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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

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