# １回目　導入回＋文字列

## 第1回授業：C++導入とCとの違い（入出力・文字列・書式制御）

### 🎯 目標

C言語の知識をもとに、C++の基本文法と文字列処理を理解する。

- `<span class="editor-theme-code">iostream</span>`<span style="white-space: pre-wrap;"> を使った標準入出力を理解する。</span>
- `<span class="editor-theme-code">std::string</span>`<span style="white-space: pre-wrap;"> と </span>`<span class="editor-theme-code">char[]</span>`<span style="white-space: pre-wrap;"> の違いを学ぶ。</span>
- `<span class="editor-theme-code">cout</span>`<span style="white-space: pre-wrap;"> による書式制御を体験する。</span>

---

## 🧩 授業構成（90分想定）

<table id="bkmrk-%E6%99%82%E9%96%93%E5%86%85%E5%AE%B9%E3%83%9D%E3%82%A4%E3%83%B3%E3%83%880%E3%80%9C10%E5%88%86c%E3%81%A8c%2B%2B%E3%81%AE%E9%96%A2"><colgroup><col></col><col></col><col></col></colgroup><tbody><tr><th>時間

</th><th>内容

</th><th>ポイント

</th></tr><tr><td>0〜10分

</td><td>CとC++の関係

</td><td>「C++はCの拡張」だが、文字列処理や入出力が高機能であることを紹介。

</td></tr><tr><td>10〜30分

</td><td>入出力の基本

</td><td>`<span class="editor-theme-code">#include <iostream></span>`

、

`<span class="editor-theme-code">std::cout</span>`

<span style="white-space: pre-wrap;"> / </span>

`<span class="editor-theme-code">std::cin</span>`

<span style="white-space: pre-wrap;"> の使い方。</span>

`<span class="editor-theme-code">using namespace std;</span>`

<span style="white-space: pre-wrap;"> の意味。</span>

</td></tr><tr><td>30〜55分

</td><td>文字列の扱い

</td><td>`<span class="editor-theme-code">char[]</span>`

<span style="white-space: pre-wrap;"> と </span>

`<span class="editor-theme-code">std::string</span>`

<span style="white-space: pre-wrap;"> の違いを説明。代入・連結・比較などを実演。</span>

</td></tr><tr><td>55〜80分

</td><td>書式制御

</td><td>`<span class="editor-theme-code">std::setw</span>`

、

`<span class="editor-theme-code">std::setfill</span>`

、

`<span class="editor-theme-code">std::fixed</span>`

、

`<span class="editor-theme-code">std::setprecision</span>`

<span style="white-space: pre-wrap;"> など </span>

`<span class="editor-theme-code"><iomanip></span>`

<span style="white-space: pre-wrap;"> の使用。表形式出力の練習。</span>

</td></tr><tr><td>80〜90分

</td><td>まとめ・課題説明

</td><td>文字列と入出力のまとめ。次回予告：「関数と参照」へ。

</td></tr></tbody></table>

---

## 🧠 授業のねらい

- C言語で扱いづらかった「文字列」や「入出力フォーマット」がC++で簡潔に扱えることを体感させる。
- 今後の授業で使う`<span class="editor-theme-code">std::string</span>`を習得し、OOPでのクラス利用にもスムーズに入れるようにする。

---

## 📘 板書・スライド例

### Cの例（文字列と出力）

```c
#include <stdio.h>
int main() {
    char name[32];
    printf("Input your name: ");
    scanf("%s", name);
    printf("Hello, %s!\n", name);
    return 0;
}
```

### C++の例（std::string版）

```cpp
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Input your name: ";
    cin >> name;
    cout << "Hello, " << name << "!" << endl;
    return 0;
}
```

<span style="white-space: pre-wrap;">💬 </span>****ポイント****

- `<span class="editor-theme-code">string</span>`<span style="white-space: pre-wrap;"> は動的にサイズが変わる安全な文字列クラス。</span>
- 代入・連結・比較が簡単。

```cpp
string a = "Hello";
string b = "World";
string c = a + ", " + b;
if (a == "Hello") cout << c << endl;
```

<span style="white-space: pre-wrap;">💡 </span>****比較:****

<table id="bkmrk-%E6%93%8D%E4%BD%9Cc-%28char%5B%5D%29c%2B%2B-%28std"><colgroup><col></col><col></col><col></col></colgroup><tbody><tr><th>操作

</th><th>C (char\[\])

</th><th>C++ (std::string)

</th></tr><tr><td>代入

</td><td>`<span class="editor-theme-code">strcpy(s, "abc")</span>`

</td><td>`<span class="editor-theme-code">s = "abc";</span>`

</td></tr><tr><td>結合

</td><td>`<span class="editor-theme-code">strcat(a, b)</span>`

</td><td>`<span class="editor-theme-code">a += b;</span>`

</td></tr><tr><td>比較

</td><td>`<span class="editor-theme-code">strcmp(a, b) == 0</span>`

</td><td>`<span class="editor-theme-code">a == b</span>`

</td></tr><tr><td>長さ

</td><td>`<span class="editor-theme-code">strlen(a)</span>`

</td><td>`<span class="editor-theme-code">a.size()</span>`

</td></tr></tbody></table>

---

## ✏️ 書式制御の例

```cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    double pi = 3.14159265;
    cout << fixed << setprecision(3);
    cout << "pi = " << pi << endl;

    cout << setw(10) << setfill('-') << 42 << endl;
    return 0;
}
```

<span style="white-space: pre-wrap;">💬 </span>****ポイント****

- `<span class="editor-theme-code"><iomanip></span>`<span style="white-space: pre-wrap;"> によりCの</span>`<span class="editor-theme-code">printf</span>`に近い整形出力が可能。
- `<span class="editor-theme-code">setw()</span>`：桁幅指定、`<span class="editor-theme-code">setfill()</span>`：埋め文字、`<span class="editor-theme-code">setprecision()</span>`：小数点桁数指定。

---

## 🧩 演習課題

1. 名前と年齢を入力し、整形して出力するプログラムを作成。  
    ```
    Name: _____
    Age : __ years old
    ```
2. `<span class="editor-theme-code">std::string</span>`を使って2つの単語を入力し、結合して表示する。
3. `<span class="editor-theme-code"><iomanip></span>`を使って3つの数値を整列表示する。

---

## 💬 授業後課題（宿題）

1. `<span class="editor-theme-code">std::string</span>`と`<span class="editor-theme-code">char[]</span>`の違いを、3つの観点（代入・結合・安全性）で説明せよ。
2. 書式制御を使って表のようなスコア表示を作るプログラムを書け。

---

## 🔎 次回予告

> ****第2回：関数と参照・オーバーロード****  
> C++独自の関数設計（参照渡し・デフォルト引数）を学び、関数の柔軟性を理解する。

# 補足資料

## 第1回授業補足編：書式制御とfmtライブラリ入門

### 🎯 目的

C++における出力整形の幅を広げ、より実践的なフォーマット出力を理解する。

- `<span class="editor-theme-code"><iomanip></span>`による標準的な書式指定を整理。
- `<span class="editor-theme-code">std::format</span>`（C++20）や`<span class="editor-theme-code">fmt</span>`ライブラリの書式構文を紹介。
- `<span class="editor-theme-code">printf</span>`との対応関係を示し、Cからの移行をスムーズにする。

---

## 🧩 &lt;iomanip&gt;による標準フォーマットまとめ

<table id="bkmrk-%E6%93%8D%E4%BD%9C%E6%93%8D%E4%BD%9C%E9%96%A2%E6%95%B0%E5%8A%B9%E6%9E%9C%E4%BD%BF%E7%94%A8%E4%BE%8B%E6%A1%81%E5%B9%85%E6%8C%87%E5%AE%9Asetw%28"><colgroup><col></col><col></col><col></col><col></col></colgroup><tbody><tr><th>操作

</th><th>操作関数

</th><th>効果

</th><th>使用例

</th></tr><tr><td>桁幅指定

</td><td>`<span class="editor-theme-code">setw(n)</span>`

</td><td>指定幅に右寄せ

</td><td>`<span class="editor-theme-code">cout << setw(5) << 42;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">42</span>`

</td></tr><tr><td>左寄せ

</td><td>`<span class="editor-theme-code">left</span>`

</td><td>出力を左寄せ

</td><td>`<span class="editor-theme-code">cout << left << setw(5) << 42;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">42</span>`

</td></tr><tr><td>埋め文字

</td><td>`<span class="editor-theme-code">setfill(c)</span>`

</td><td>空白を指定文字で埋める

</td><td>`<span class="editor-theme-code">cout << setfill('-') << setw(5) << 42;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">---42</span>`

</td></tr><tr><td>進数指定

</td><td>`<span class="editor-theme-code">dec</span>`

<span style="white-space: pre-wrap;"> / </span>

`<span class="editor-theme-code">hex</span>`

<span style="white-space: pre-wrap;"> / </span>

`<span class="editor-theme-code">oct</span>`

</td><td>10進・16進・8進表示

</td><td>`<span class="editor-theme-code">cout << hex << 255;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">ff</span>`

</td></tr><tr><td>小数点桁数

</td><td>`<span class="editor-theme-code">setprecision(n)</span>`

</td><td>小数点以下の桁数指定

</td><td>`<span class="editor-theme-code">cout << fixed << setprecision(2) << 3.14159;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">3.14</span>`

</td></tr><tr><td>浮動小数点表記

</td><td>`<span class="editor-theme-code">fixed</span>`

<span style="white-space: pre-wrap;"> / </span>

`<span class="editor-theme-code">scientific</span>`

</td><td>固定小数点／指数表記

</td><td>`<span class="editor-theme-code">cout << scientific << 0.00123;</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">1.23e-03</span>`

</td></tr></tbody></table>

<span style="white-space: pre-wrap;">💡 </span>****ポイント****

- `<span class="editor-theme-code">setw()</span>`<span style="white-space: pre-wrap;"> は </span>****次の出力にのみ有効****（毎回指定が必要）。
- `<span class="editor-theme-code">setfill()</span>`<span style="white-space: pre-wrap;"> は </span>****継続して有効****。
- `<span class="editor-theme-code">fixed</span>`<span style="white-space: pre-wrap;"> を指定しない場合、整数部分や小数点以下が自動的に最適化される。</span>

---

## 🧠 応用例：表形式出力

```cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << left << setw(10) << "Name" << right << setw(8) << "Score" << endl;
    cout << setfill('-') << setw(18) << "" << endl;
    cout << setfill(' ');

    cout << left << setw(10) << "Alice" << right << setw(8) << 95 << endl;
    cout << left << setw(10) << "Bob"   << right << setw(8) << 87 << endl;
    cout << left << setw(10) << "Chris" << right << setw(8) << 100 << endl;
}
```

出力例：

```
Name         Score
------------------
Alice           95
Bob             87
Chris          100
```

---

## 🧩 std::format (C++20）とfmtライブラリ

C++20以降では、Python風の書式指定が可能な`<span class="editor-theme-code">std::format</span>`が導入されています。旧環境では`<span class="editor-theme-code">fmt</span>`ライブラリ（[https://github.com/fmtlib/fmt](https://github.com/fmtlib/fmt)）を使用できます。

### 使い方例

```cpp
#include <format> // C++20以降
#include <iostream>
using namespace std;

int main() {
    string name = "Alice";
    int score = 95;
    double rate = 0.8765;

    cout << format("Name: {:<10} | Score: {:>4d} | Rate: {:.2f}\n", name, score, rate);
}
```

出力：

```
Name: Alice      | Score:   95 | Rate: 0.88
```

<table id="bkmrk-%E6%9B%B8%E5%BC%8F%E6%8C%87%E5%AE%9A%E5%AD%90%E6%84%8F%E5%91%B3%E4%BE%8B%3A%3C10%E5%B7%A6%E5%AF%84%E3%81%9B%E3%83%BB%E5%B9%8510%7B"><colgroup><col></col><col></col><col></col></colgroup><tbody><tr><th>書式指定子

</th><th>意味

</th><th>例

</th></tr><tr><td>`<span class="editor-theme-code">:<10</span>`

</td><td>左寄せ・幅10

</td><td>`<span class="editor-theme-code">{: <10}</span>`

<span style="white-space: pre-wrap;"> → 左寄せ</span>

</td></tr><tr><td>`<span class="editor-theme-code">:>10</span>`

</td><td>右寄せ・幅10

</td><td>`<span class="editor-theme-code">{: >10}</span>`

<span style="white-space: pre-wrap;"> → 右寄せ</span>

</td></tr><tr><td>`<span class="editor-theme-code">:.2f</span>`

</td><td>小数点以下2桁

</td><td>`<span class="editor-theme-code">{:.2f}</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">3.14</span>`

</td></tr><tr><td>`<span class="editor-theme-code">:06d</span>`

</td><td>6桁ゼロ埋め

</td><td>`<span class="editor-theme-code">{:06d}</span>`

<span style="white-space: pre-wrap;"> → </span>

`<span class="editor-theme-code">000042</span>`

</td></tr><tr><td>`<span class="editor-theme-code">{}</span>`

</td><td>自動推論

</td><td>デフォルト出力

</td></tr></tbody></table>

### 💬 fmtライブラリでの使用例（C++17以前）

```cpp
#include <fmt/core.h>
#include <string>

int main() {
    std::string name = "Bob";
    int age = 20;
    fmt::print("Hello, {}! You are {} years old.\n", name, age);
}
```

出力：

```
Hello, Bob! You are 20 years old.
```

---

## 🔍 比較：Cのprintf関数との違い

<table id="bkmrk-%E6%93%8D%E4%BD%9Cprintfiostreamfmt-"><colgroup><col></col><col></col><col></col><col></col></colgroup><tbody><tr><th>操作

</th><th>printf

</th><th>iostream

</th><th>fmt / format

</th></tr><tr><td>書式構文

</td><td>`<span class="editor-theme-code">%d %f %s</span>`

</td><td>関数チェーン形式

</td><td>`<span class="editor-theme-code">{}</span>`

<span style="white-space: pre-wrap;"> ベースのテンプレート式</span>

</td></tr><tr><td>型安全性

</td><td>❌ 弱い

</td><td>✅ 強い

</td><td>✅ 強い

</td></tr><tr><td>可読性

</td><td>△

</td><td>○

</td><td>◎

</td></tr><tr><td>柔軟性

</td><td>○

</td><td>○

</td><td>◎（動的組み立て可）

</td></tr></tbody></table>

---

## 💡 まとめ

- `<span class="editor-theme-code"><iomanip></span>`<span style="white-space: pre-wrap;"> は既存Cライクコードに馴染む標準方式。</span>
- `<span class="editor-theme-code">std::format</span>`<span style="white-space: pre-wrap;"> / </span>`<span class="editor-theme-code">fmt</span>`<span style="white-space: pre-wrap;"> は現代的で読みやすく、ゲームデバッグ出力などに最適。</span>
- どちらも「****Cのprintfより安全で拡張性が高い****」という点を体験させるのが狙い。

---

### 🔎 次にやるとよい演習

1. 数値を右寄せ・左寄せしてスコア表を整形。
2. `<span class="editor-theme-code">fmt::print</span>`<span style="white-space: pre-wrap;"> を使ってデバッグメッセージを出すツール関数を作る。</span>
3. `<span class="editor-theme-code">std::format</span>`<span style="white-space: pre-wrap;"> でログ出力を作り、</span>`<span class="editor-theme-code">cout</span>`版と比較する。