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)

第4章 ループ、配列、ベクトルの使用

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

出力の書き込みや入力の取得を行うコードを作成できる。 これまで1つの数値を取得してきた。 この章では、コンテナを使って複数の数値を保存する方法を示す。 C++のコンテナは、要素を保存する一般的なデータ構造のためのテンプレートだ。 C++には配列やベクトルのような順序付きコンテナと、検索テーブルを構築できる連想コンテナなど、様々な種類のコンテナがある。 後者の例は第15章で見る。

この章では、コンテナの初期化と使用方法を学ぶ。 また、最大要素などコンテナのプロパティを発見する方法も学ぶ。 ループなどC++の他の有用な要素についても学ぶ。 配列の説明では多くの新機能を紹介する。 ベクトルに到達する頃には類似性から概念を把握し、試すべきことが推測できるかもしれない。

この章が終わる頃には、C++についてさらに多くのことを知り、より詳細なプログラムを書く準備が整っているだろう。 第5章では、標準ライブラリの様々なアルゴリズムを使ってコンテナ内の要素を分析する方法について学ぶ。

ループを使った複数の数値入力

第2章では、単一の数字を入力するを学んだ。 複数の数字を取得する方法について考えてみよう。 複数の数字を取得するにはどうすればよいか? 例4-1に示すように、初期の入力コードを基に、2つの変数を宣言して2つの数字を取得する方法がある。

例 4-1. 複数の数値を入力する試み
#include <iostream>

int main()
{
    double number1{};
    double number2{};
    std::cout << "Please enter a number.\n>";
    std::cin >> number1;
    std::cout << number1 << '\n';
    std::cout << "Please enter another number.\n>";
    std::cin >> number2;
    std::cout << number2 << '\n';
}

このコードを試して2つの数字を入力すると、それらが表示されるはずだ:

Please enter a number.
>3 1
3
Please enter another number.
>-7 2
-7
1

数字を入力してEnterキーを押す。

2

別の数字を入力し、Enterキーを押す。

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