フリーキーズ | 独学プログラミング

C++イテレータの基礎!begin/endから範囲for文まで

リンドくん

リンドくん

たなべ先生、C++でvectorとかarrayを使うとき、よく「イテレータ」って言葉を聞くんですけど、これって何なんですか?

たなべ

たなべ

イテレータは、コンテナの要素を順番にアクセスするためのツールなんだ。
まるで本のページをめくるような感じで、データを一つずつ見ていけるんだよ。

プログラミングを学び始めた方にとって、C++のイテレータは最初少し難しく感じるかもしれません。
しかし、イテレータを理解することで、配列やvectorなどのコンテナを効率的に操作できるようになります。

実際、現代のC++開発では、イテレータを使った処理が標準的になっており、特に範囲for文は日常的に使われています。
従来のfor文と比べて、コードがシンプルで読みやすくなるため、多くの開発者に愛用されているのです。

この記事では、C++のイテレータについて、基本概念から実践的な使い方まで、プログラミング初心者の方でも理解できるよう丁寧に解説していきます。
「なんとなく聞いたことはあるけど...」という方も、この機会にしっかりとマスターしていきましょう!

プログラミング学習でお悩みの方へ

HackATAは、エンジニアを目指す方のためのプログラミング学習コーチングサービスです。 経験豊富な現役エンジニアがあなたの学習をサポートします。

✓ 質問し放題

✓ β版公開中(2025年内の特別割引)

HackATAの詳細を見る

イテレータとは何か?その基本概念を理解しよう

リンドくん

リンドくん

でも先生、なぜイテレータが必要なんですか?普通のfor文じゃダメなんですか?

たなべ

たなべ

確かにfor文でもできるけど、イテレータを使うとより安全で効率的なコードが書けるんだ。
特にSTLのコンテナを扱うときは、イテレータの方が圧倒的に便利なんだよ。

イテレータの基本概念

イテレータ(Iterator)とは、コンテナ(配列、vector、listなど)の要素を順次アクセスするためのオブジェクトです。
簡単に言えば、「今どの要素を指しているか」を示すポインタのような存在と考えていただけると分かりやすいでしょう。

イテレータの最大の特徴は以下の通りです。

  • 統一されたインターフェース - 異なるコンテナでも同じ方法でアクセス可能
  • 安全性の向上 - 範囲外アクセスを防げる
  • 効率性 - コンテナに最適化された方法でアクセス
  • STLアルゴリズムとの連携 - sort、findなどの関数で活用可能

なぜイテレータが重要なのか

従来のインデックス(添字)を使ったアクセス方法と比較してみましょう。

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    // 従来のインデックスを使った方法
    for (size_t i = 0; i < numbers.size(); ++i)
    {
        std::cout << numbers[i] << " ";
    }
    std::cout << std::endl;
    
    // イテレータを使った方法
    for (auto it = numbers.begin(); it != numbers.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

一見すると従来の方法の方がシンプルに見えるかもしれません。
しかし、イテレータを使うことで、listやsetといったインデックスアクセスができないコンテナでも同じ書き方ができるという大きなメリットがあります。

begin()とend()の使い方をマスターしよう

リンドくん

リンドくん

begin()end()って、よく見るんですけど、どういう意味なんですか?

たなべ

たなべ

begin()最初の要素を指すイテレータend()最後の要素の次を指すイテレータなんだ。
この「次を指す」ってのがポイントで、実際には存在しない位置を表しているんだよ。

begin()とend()の基本

C++のコンテナには、以下の2つの重要なメンバ関数があります。

  • begin() - コンテナの最初の要素を指すイテレータを返す
  • end() - コンテナの最後の要素の「次」を指すイテレータを返す

重要なのは、end()は実際の要素を指しているわけではなく、「ここで終わり」を示すマーカーのような役割を果たしていることです。

実践的な使用例

#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> fruits = {"apple", "banana", "cherry", "date"};
    
    // イテレータを使った基本的なループ
    std::cout << "フルーツ一覧:" << std::endl;
    for (auto it = fruits.begin(); it != fruits.end(); ++it)
    {
        std::cout << "- " << *it << std::endl;
    }
    
    // const_iteratorを使った読み取り専用アクセス
    std::cout << "\n読み取り専用アクセス:" << std::endl;
    for (auto it = fruits.cbegin(); it != fruits.cend(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

イテレータの種類と特徴

C++には複数の種類のイテレータがあります。

  • iterator - 読み取りと書き込みが可能
  • const_iterator - 読み取り専用(要素の変更不可)
  • reverse_iterator - 逆順でアクセス可能
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> numbers = {10, 20, 30, 40, 50};
    
    // 通常のイテレータで値を変更
    for (auto it = numbers.begin(); it != numbers.end(); ++it)
    {
        *it *= 2;  // 各要素を2倍にする
    }
    
    // reverse_iteratorで逆順にアクセス
    std::cout << "逆順表示: ";
    for (auto it = numbers.rbegin(); it != numbers.rend(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

このように、用途に応じて適切なイテレータを選択することで、より安全で効率的なコードを書くことができます。

範囲for文で効率的なコード作成

リンドくん

リンドくん

さっきのコード、もっと簡単に書く方法はないんですか?イテレータの書き方、ちょっと複雑で...

たなべ

たなべ

C++11から導入された範囲for文を使えば、もっとシンプルに書けるんだ。
これは本当に便利で、今では標準的な書き方になっているよ。

範囲for文の基本

範囲for文は、C++11で導入された機能で、コンテナの全要素に対して簡潔にループ処理を行うことができます。
内部的にはイテレータを使用していますが、記述がとてもシンプルになります。

基本的な構文は以下の通りです。

for (データ型 変数名 : コンテナ)
{
    // 処理
}

使用例

#include <vector>
#include <iostream>
#include <string>

int main()
{
    std::vector<std::string> cities = {"Tokyo", "Osaka", "Kyoto", "Hiroshima"};
    
    // 範囲for文で読み取り
    std::cout << "日本の都市:" << std::endl;
    for (const auto& city : cities)
    {
        std::cout << "- " << city << std::endl;
    }
    
    // 値を変更する場合は参照を使用
    std::vector<int> scores = {85, 92, 78, 96, 88};
    std::cout << "\n変更前のスコア: ";
    for (const auto& score : scores)
    {
        std::cout << score << " ";
    }
    std::cout << std::endl;
    
    // 各スコアに5点加算
    for (auto& score : scores)
    {
        score += 5;
    }
    
    std::cout << "変更後のスコア: ";
    for (const auto& score : scores)
    {
        std::cout << score << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

範囲for文の使い分け

重要なのは、用途に応じて適切な記述方法を選択することです。

#include <vector>
#include <iostream>

int main()
{
    std::vector<std::string> messages = {"Hello", "World", "C++", "Programming"};
    
    // 1. 読み取り専用の場合(const参照)
    for (const auto& msg : messages)
    {
        std::cout << msg << " ";
    }
    std::cout << std::endl;
    
    // 2. 値を変更したい場合(非const参照)
    for (auto& msg : messages)
    {
        msg += "!";  // 各文字列に「!」を追加
    }
    
    // 3. コピーでも良い場合(小さなデータ型)
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    for (auto num : numbers)  // 参照ではなくコピー
    {
        std::cout << num * 2 << " ";  // 元の値は変更されない
    }
    std::cout << std::endl;
    
    return 0;
}

使い分けのポイント

  • const auto& - 読み取り専用、効率的(コピーしない)
  • auto& - 値を変更したい場合
  • auto - 小さなデータ型でコピーしても問題ない場合

このように、範囲for文を使うことで、従来のイテレータよりもはるかに読みやすく、書きやすいコードが実現できます。

様々なコンテナでのイテレータ活用法

リンドくん

リンドくん

vectorだけじゃなくて、他のコンテナでもイテレータって使えるんですか?

たなべ

たなべ

もちろん!STLのコンテナは統一されたインターフェースを持っているから、どのコンテナでも同じようにイテレータが使えるんだ。
これがSTLの素晴らしいところなんだよ。

異なるコンテナでの統一された操作

C++のSTL(Standard Template Library)では、様々なコンテナが統一されたイテレータインターフェースを提供しています。
これにより、コンテナの種類が変わっても、同じ方法でデータにアクセスできます。

#include <vector>
#include <list>
#include <set>
#include <map>
#include <iostream>

int main()
{
    // vector(動的配列)
    std::vector<int> vec = {10, 20, 30, 40, 50};
    std::cout << "vector: ";
    for (const auto& value : vec)
    {
        std::cout << value << " ";
    }
    std::cout << std::endl;
    
    // list(双方向リスト)
    std::list<int> lst = {100, 200, 300, 400, 500};
    std::cout << "list: ";
    for (const auto& value : lst)
    {
        std::cout << value << " ";
    }
    std::cout << std::endl;
    
    // set(重複なしの集合)
    std::set<int> st = {5, 1, 9, 3, 7};
    std::cout << "set: ";
    for (const auto& value : st)
    {
        std::cout << value << " ";  // 自動的にソートされて表示される
    }
    std::cout << std::endl;
    
    return 0;
}

mapコンテナでの特殊な使い方

mapコンテナでは、キーと値のペアを扱うため、少し特殊な記述が必要です。

#include <map>
#include <iostream>
#include <string>

int main()
{
    std::map<std::string, int> scores = {
        {"Alice", 95},
        {"Bob", 87},
        {"Charlie", 92},
        {"Diana", 88}
    };
    
    // C++17以降の構造化束縛を使用
    std::cout << "成績表:" << std::endl;
    for (const auto& [name, score] : scores)
    {
        std::cout << name << ": " << score << "点" << std::endl;
    }
    
    // C++14以前の書き方
    std::cout << "\n従来の書き方:" << std::endl;
    for (const auto& pair : scores)
    {
        std::cout << pair.first << ": " << pair.second << "点" << std::endl;
    }
    
    return 0;
}

コンテナ選択の指針

それぞれのコンテナには特徴があり、用途に応じて最適なものを選択することが重要です。

  • vector - 高速なランダムアクセス、末尾への追加が効率的
  • list - 任意の位置への挿入・削除が効率的
  • set - 重複なし、自動ソート、検索が高速
  • map - キーと値のペア、検索が高速

どのコンテナを選んでも、イテレータによる統一されたアクセス方法が使えるのがC++の大きな利点です。

STLアルゴリズムとイテレータの連携

リンドくん

リンドくん

イテレータって、ループ以外にも使い道があるんですか?

たなべ

たなべ

実は、STLにはsort、find、countなどの便利な関数がたくさんあって、これらはイテレータと組み合わせて使うんだ。
自分でループを書かなくても、高効率な処理ができるんだよ。

STLアルゴリズムの基本

STL(Standard Template Library)には、多くの便利なアルゴリズム関数が用意されています。
これらの関数は、イテレータを引数として受け取り、効率的な処理を提供します。

#include <vector>
#include <algorithm>
#include <iostream>
#include <numeric>

int main()
{
    std::vector<int> numbers = {64, 25, 12, 22, 11, 90, 5, 77, 30};
    
    // 1. ソート
    std::sort(numbers.begin(), numbers.end());
    std::cout << "ソート後: ";
    for (const auto& num : numbers)
    {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    
    // 2. 検索
    auto found = std::find(numbers.begin(), numbers.end(), 22);
    if (found != numbers.end())
    {
        std::cout << "22が見つかりました!位置: " << (found - numbers.begin()) << std::endl;
    }
    
    // 3. 条件に合う要素の数をカウント
    int count = std::count_if(numbers.begin(), numbers.end(), [](int n)
    {
        return n > 30;
    });
    std::cout << "30より大きい数の個数: " << count << std::endl;
    
    // 4. 合計値の計算
    int sum = std::accumulate(numbers.begin(), numbers.end(), 0);
    std::cout << "合計値: " << sum << std::endl;
    
    return 0;
}

ラムダ式との組み合わせ

モダンC++では、ラムダ式とSTLアルゴリズムを組み合わせて、非常に表現力豊かなコードが書けます。

#include <vector>
#include <algorithm>
#include <iostream>
#include <string>

int main()
{
    std::vector<std::string> words = {"apple", "banana", "cherry", "date", "elderberry"};
    
    // 1. 条件に合う要素を変更
    std::for_each(words.begin(), words.end(), [](std::string& word)
    {
        if (word.length() > 5)
        {
            word = word + " (long)";
        }
    });
    
    // 2. 条件に合う要素のみを表示
    std::cout << "5文字以下の単語:" << std::endl;
    std::for_each(words.begin(), words.end(), [](const std::string& word)
    {
        if (word.length() <= 5)
        {
            std::cout << "- " << word << std::endl;
        }
    });
    
    // 3. 全ての要素を大文字に変換
    std::vector<std::string> fruits = {"apple", "banana", "cherry"};
    std::transform(fruits.begin(), fruits.end(), fruits.begin(), [](std::string s)
    {
        std::transform(s.begin(), s.end(), s.begin(), ::toupper);
        return s;
    });
    
    std::cout << "\n大文字変換後:" << std::endl;
    for (const auto& fruit : fruits)
    {
        std::cout << fruit << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

よくある間違いと対処法

リンドくん

リンドくん

イテレータを使ってるとエラーが出ることがあるんですけど、よくある間違いってありますか?

たなべ

たなべ

あるあるだね!特に範囲外アクセス無効なイテレータの使用は初心者がよく引っかかるポイントなんだ。
でも、これらは知っておけば簡単に避けられるよ。

よくある間違い1 空のコンテナへのアクセス

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> empty_vec;
    
    // 危険!空のコンテナに対してbegin()をデリファレンス
    // auto first = *empty_vec.begin();  // エラー!
    
    // 正しい方法:事前にチェック
    if (!empty_vec.empty())
    {
        auto first = *empty_vec.begin();
        std::cout << "最初の要素: " << first << std::endl;
    }
    else
    {
        std::cout << "コンテナが空です" << std::endl;
    }
    
    // より安全な方法:範囲for文を使用
    for (const auto& value : empty_vec)
    {
        std::cout << value << std::endl;  // 空なら何も実行されない
    }
    
    return 0;
}

よくある間違い2 無効なイテレータの使用

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    
    // イテレータを取得
    auto it = numbers.begin() + 2;  // 3を指す
    std::cout << "元の値: " << *it << std::endl;
    
    // 要素を追加すると、イテレータが無効になる可能性がある
    numbers.push_back(6);
    // std::cout << *it << std::endl;  // 危険!無効なイテレータかも
    
    // 正しい方法:操作後にイテレータを再取得
    it = numbers.begin() + 2;
    std::cout << "再取得後: " << *it << std::endl;
    
    return 0;
}

よくある間違い3 end()イテレータのデリファレンス

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> numbers = {10, 20, 30};
    
    // 危険!end()は「終端」を表すので、デリファレンスできない
    // auto value = *numbers.end();  // エラー!
    
    // 正しい方法:end()との比較に使用
    for (auto it = numbers.begin(); it != numbers.end(); ++it)
    {
        std::cout << *it << " ";
    }
    std::cout << std::endl;
    
    // 最後の要素にアクセスしたい場合
    if (!numbers.empty())
    {
        auto last = *(numbers.end() - 1);  // または numbers.back()
        std::cout << "最後の要素: " << last << std::endl;
    }
    
    return 0;
}

推奨される安全な書き方

#include <vector>
#include <iostream>
#include <algorithm>

int main()
{
    std::vector<int> numbers = {15, 32, 8, 47, 23, 91, 6};
    
    // 1. 安全なサイズチェック
    if (numbers.size() >= 3)
    {
        std::cout << "3番目の要素: " << numbers[2] << std::endl;
    }
    
    // 2. at()メソッドで境界チェック付きアクセス
    try 
    {
        std::cout << "10番目の要素: " << numbers.at(9) << std::endl;
    }
    catch (const std::out_of_range& e)
    {
        std::cout << "範囲外アクセスエラー: " << e.what() << std::endl;
    }
    
    // 3. STLアルゴリズムでの安全な検索
    auto found = std::find(numbers.begin(), numbers.end(), 47);
    if (found != numbers.end())
    {
        std::cout << "47が見つかりました!位置: " << (found - numbers.begin()) << std::endl;
    }
    else
    {
        std::cout << "47は見つかりませんでした" << std::endl;
    }
    
    // 4. 範囲for文が最も安全
    std::cout << "すべての要素: ";
    for (const auto& num : numbers)
    {
        std::cout << num << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

これらの注意点を意識することで、より安全で信頼性の高いC++コードを書くことができます。

まとめ

リンドくん

リンドくん

イテレータって、最初は難しそうに思えたけど、使いこなせると本当に便利ですね!

たなべ

たなべ

その通り!特に範囲for文は日常的に使うようになるよ。
イテレータを理解することで、C++のSTLを最大限に活用できるようになるんだ。これからも積極的に使っていってね!

この記事では、C++のイテレータについて、基本概念から実践的な活用法まで幅広く解説してきました。
イテレータは最初は少し複雑に感じるかもしれませんが、一度理解すれば、C++プログラミングの効率性と安全性を大幅に向上させる強力なツールとなります。

長年プログラミング教育に携わってきましたが、イテレータを理解した学習者は、その後のC++学習がスムーズに進む傾向があります。
ぜひ実際のプロジェクトでイテレータを積極的に使用し、その便利さを体感してください。

この記事をシェア

関連するコンテンツ