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)

第5章 標準ライブラリのアルゴリズムを使う

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

第4章では、入力によっていくつかの数値を得て、配列の中で最大の値を発見した。 この章では、今度は標準ライブラリの関数を使って同じことを行う。 また、コードの組織化の仕方についても示す。そうすれば、異なるプログラムで関数を再利用できる。

数字に意味を持たせよう:株価だ。 株価のセットがあれば、最大値や最小値、その他の性質を発見できる。 この本の残りの部分では、株価をシミュレートする方法、ファイルから読み取る方法、小さな取引シミュレーションゲームを作る方法を示す。

複数の数値をベクトルに入れる(再び)

前の章では、数値を配列に入れその後ベクターに入れたベクターは可変数の値を格納できるため、多くの人がデフォルトで選ぶ連続コンテナだ。

第4章のコードを再利用しやすいように再構成する。 これまで書いたソースファイルはそれぞれ独自のメイン関数を持っていた。 アプリケーションはメイン関数を一つしか持てないが、複数のソースファイルを含めることはできる。 このセクションでは、拡張子.cppのソースファイルを二つと、拡張子.hのヘッダファイルを一つ書く。 一つのソースファイルには通常のメイン関数を置き、もう一つのソースファイルのコードを使う。このコードは後の章でも再利用する。

ヒント

ベストプラクティスとして、ソースファイルには.cpp拡張子を使うのが良い。必須ではないし、.cc.cxxなどを使う人もいる。しかし一貫性はコードの構造を明確にする。ヘッダの拡張子も重要ではなく、.h ではなく.hppを使うことが多い。ここではソースファイルに.cpp、ヘッダに.hを使うが、他の慣習も見かけるかもしれない。 これまで見てきたように、標準ライブラリのヘッダには拡張子が付いていない。つまり拡張子がない場合、標準ライブラリを使用していると判断できる。

ヘッダーファイル()に宣言を記述すれば、そのヘッダーをインクルードする他のソースファイルでそれらを利用できる。 使用する関数やオブジェクトをソースファイル内で直接宣言することも可能だ。 しかし、同じ宣言を複数のソースファイルで必要とする場合もある。 各ソースファイル()に同じコードを追加するのは合理的ではない。繰り返しのコードは煩わしく、エラーの原因になりやすい。ヘッダーファイルを使用する方がはるかに良い方法だ。

ヘッダファイルをインクルードすると、そのファイルのコードが#include行の場所にコピーされる。だからコードをコピー&ペーストする必要はない。 #includeは「ヘッダの内容をここにコピーせよ」と言っているようなものだ。図 5-1 のように。

Diagram illustrating how a header file's contents are copied into the source code during pre-processing when using #include.
図 5-1. ヘッダのインクルード

新しいファイルを作成し、input.h という名前で保存する。 このファイルで、以前に書いたget_number関数を宣言する。 例 4-11 を振り返り、関数get_number の先頭部分に注目する。 これが関数のシグネチャだ。 この部分だけを使い、セミコロンで終わらせれば、関数を宣言していることになる。つまり、どこかに一致する定義があると言っているのだ。 ...

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