Skip to Content
C++の紹介 (Japanese Edition)
book

C++の紹介 (Japanese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
4h 30m
Japanese
O'Reilly Media, Inc.
Content preview from C++の紹介 (Japanese Edition)

第6章 Lambda式 と範囲ライブラリ

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

第5章ではいくつかのアルゴリズムを使った。関数を受け取るものもあり、負の数を発見するために手書きの負の関数を使った。名前付き関数を使うのも構わないが、C++には別の方法がある。 例えば、std::greater を含む関数オブジェクトも使った。 C++ はアルゴリズムに関数を提供する別の方法も用意している:ラムダ式つまり無名関数だ。 ラムダ式は短い関数を簡潔に記述する手段であり、標準アルゴリズムの呼び出し内で直接述語関数やその他の関数を記述できる。

この章ではLambdaの書き方を示し、アルゴリズム使用のさらなる練習を提供する。 前章では範囲アルゴリズムをいくつか使用した。 範囲はビューを含む複数のライブラリ機能も提供するため、それらの使用法を確認する。また標準ライブラリアルゴリズム以外でのLambda使用法も学ぶ。

第5章では5-3でmain関数内にget_prices関数を記述した。 これはinput.cppで定義したget_number関数を利用していた。 本章では、より一般性のあるget_prices関数を作成する。これは後続の章やいくつかの取引戦略で再利用可能だ。 大金を稼げるわけではないが、C++のさらなる側面を学べる。 本章終了時には、分析用と入力用のソースファイルがより有用になる。そこから第7章では、価格を手入力する代わりに自動生成する方法を学ぶ。

Lambda式を用いた負の数の除去

第5章では、の負の数を除去するためにerase_ifを使用した

auto erased = std::erase_if(prices, stock_prices::negative);
std::cout <<  erased << " prices below zero\n";

負の数値除去関数はanalysis.hファイルに記述した:

inline bool negative(double value)
{
    return value < 0.0;
}

別の方法として、無名関数(Lambda関数)を使う方法がある。1erase_ifの呼び出し内で直接使用する方法だ。 これまで書いてきた関数とは異なり、ラムダ関数には名前がない。そのため「匿名関数」とも呼ばれ、その戻り値の型は定義から推論される。 残るはパラメータと実装だ:

(double value){ return value < 0.0; }

これでほぼLambda式だ。あと一つ必要な部分がある。

ラムダ関数は、参照または値渡しで、周辺スコープの変数を使用できる。 変数が必要な場合は、先頭に角括弧[]を付ける。 値が負かどうかを検出するのに他の変数は不要だから、括弧は空のままにしておく:

[](double value){ return value < 0.0; }

このラムダ式を直接erase_if に渡せる:

auto erased = std::erase_if(prices, [](double value){ return value < 0.0; });

名前付き関数negativeを使うとコードの動作が明確になるが、実装をアルゴリズム呼び出しに直接記述することにも利点がある。 例えば、関数実装を別箇所を探す必要なく、コードが何をしているかを正確に把握できる。 ラムダ式は短い関数を簡潔に定義する方法だ。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

エージェントメッシュ (Japanese Edition)

エージェントメッシュ (Japanese Edition)

Eric Broda, Davis Broda

Publisher Resources

ISBN: 0642572352653