1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Hello World あたたたた 12日目 C++編

Last updated at Posted at 2025-12-11

この記事は Hello World あたたたた Advent Calendar 2025 の12日目の記事です。

今日は C++ で Hello World あたたたた を実装して解説していきます。

そもそも「Hello World あたたたた」が何かは 1日目の記事 をご覧ください。

コーディング例

paiza.io で実際に動かしてみることができます。

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

int main() {
    std::srand((unsigned int)std::time(nullptr)); // 乱数の初期化

    std::string hako = "";     // 出力した文字をためていく変数
    bool running = true;       // ループ継続フラグ

    while (running) {

        // 0 または 1 を生成
        int x = std::rand() % 2;

        // あ or た を選択(UTF-8 で 3 バイト)
        std::string ch = (x == 0) ? "あ" : "た";

        // 改行なしで出力
        std::cout << ch;

        // hako に追加
        hako += ch;

        // 末尾が "あたたたた" なら終了
        if (hako.size() >= std::string("あたたたた").size() &&
            hako.compare(hako.size() - std::string("あたたたた").size(),
                         std::string("あたたたた").size(),
                         "あたたたた") == 0) {

            std::cout << "\nお前はもう死んでいる\n";
            running = false;
        }
    }

    return 0;
}

コードと文法の解説

コードの冒頭

  • #include <library_name> : 必要なライブラリを読み込む
    • iostream : 標準入出力ライブラリ。
    • string : std::string 型を使うため。C 言語の char[] より安全で便利な文字列クラス
    • cstdlib : C 標準ライブラリの の C++ 版。乱数を使うのに使う
    • ctime : std::time() を使うため。乱数の seed 初期化に使う
  • int main() { 処理 } : C++ プログラムの エントリーポイント。実行はこの main 関数から始まります。

変数の宣言・代入

std::string hako = "";
bool running = true;
int x = 0;
  • C++ は静的型付け言語のため、変数には必ず型を付けます。
  • std::string … 可変文字列(C の char 配列より安全)
  • bool … 真偽値
  • int … 整数

繰り返し

while (running) {
    // 処理
}

条件が true の間ループが続きます。

条件判定

if (x == 0) {
    ch = "あ";
} else {
    ch = "た";
}
  • == は比較
  • else if は else if と記述

文字の出力

std::cout << ch;
std::cout << "\n";
  • 標準出力ストリーム std::cout を使う。
  • 演算子 << で文字を出力する

文字列の連結

hako += ch;

std::string+ で連結できます。

最後の5文字を取り出して比較

hako.compare(hako.size() - target.size(), target.size(), target) == 0
  • C++ の std::string は「UTF-8 が 1 文字=1バイト」とは限らないためUTF-8 の日本語を正確に文字単位で扱うのは難しい。今回の "あたたたた"文字列全体での比較 にして簡易的に処理する。
  • compare は部分一致を比較するメソッド

乱数生成

std::srand(std::time(nullptr));
int x = std::rand() % 2;
  • std::srand … 乱数のシード設定
  • std::rand() % 2 … 0 または 1

C++ の概要と歴史

C++ の特徴

  • C の機能や特徴に加え、オブジェクト指向などを導入
  • ハードウェアを直接扱うような低水準言語としても、複雑なアプリケーションソフトウェアを開発するための上位高水準言語としても使用可能
  • 組み込み・ゲーム・金融・高速システムなど幅広く使われる

C++ の歴史

  • 開発者:Bjarne Stroustrup(ベル研究所)
  • 誕生:1983年頃
  • 名前の由来:開発中は「C with Classes」と呼ばれていた。「C に 1 を足す」という C のインクリメント ++ にかけた命名。

個人的なコメント

C言語を改良したもののイメージ。Classとかも使えるオブジェクト指向言語。Javaと近い文法なので理解しやすい。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?