C言語の知識をもとに、C++の基本文法と文字列処理を理解する。
iostream を使った標準入出力を理解する。std::string と char[] の違いを学ぶ。cout による書式制御を体験する。| 時間 | 内容 | ポイント |
|---|---|---|
| 0〜10分 | CとC++の関係 | 「C++はCの拡張」だが、文字列処理や入出力が高機能であることを紹介。 |
| 10〜30分 | 入出力の基本 | #include <iostream> / std::cout / std::cin の使い方。 using namespace std; の意味。 |
| 30〜55分 | 文字列の扱い | char[] と std::string の違いを説明。代入・連結・比較などを実演。 |
| 55〜80分 | 書式制御 | std::setw、std::setfill、std::fixed、std::setprecision など <iomanip> の使用。表形式出力の練習。 |
| 80〜90分 | まとめ・課題説明 | 文字列と入出力のまとめ。次回予告:「関数と参照」へ。 |
std::string を習得し、OOPでのクラス利用にもスムーズに入れるようにする。#include <stdio.h> int main() { char name[32]; printf("Input your name: "); scanf("%s", name); printf("Hello, %s!\n", name); return 0; }
#include <iostream> #include <string> using namespace std; int main() { string name; cout << "Input your name: "; cin >> name; cout << "Hello, " << name << "!" << endl; return 0; }
ポイント
string は動的にサイズが変わる安全な文字列クラス。string a = "Hello"; string b = "World"; string c = a + ", " + b; if (a == "Hello") cout << c << endl;
| 操作 | C (char[]) | C++ (std::string) |
|---|---|---|
| 代入 | strcpy(s, “abc”) | s = “abc”; |
| 結合 | strcat(a, b) | a += b; |
| 比較 | strcmp(a, b) == 0 | a == b |
| 長さ | strlen(a) | a.size() |
#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; ``` }
ポイント
<iomanip> によりCの printf に近い整形出力が可能。setw():桁幅指定、setfill():埋め文字、setprecision():小数点桁数指定。std::string を使って2つの単語を入力し、結合して表示する。<iomanip> を使って3つの数値を整列表示する。std::string と char[] の違いを、3つの観点(代入・結合・安全性)で説明せよ。第2回:関数と参照・オーバーロード C++独自の関数設計(参照渡し・デフォルト引数)を学び、関数の柔軟性を理解する。
C++における出力整形の幅を広げ、より実践的なフォーマット出力を理解する。
<iomanip> による標準的な書式指定を整理。std::format(C++20)や fmt ライブラリの書式構文を紹介。printf との対応関係を示し、Cからの移行をスムーズにする。| 操作 | 操作関数 | 効果 | 使用例 |
|---|---|---|---|
| 桁幅指定 | setw(n) | 指定幅に右寄せ | cout « setw(5) « 42; → 42 |
| 左寄せ | left | 出力を左寄せ | cout « left « setw(5) « 42; → 42 |
| 埋め文字 | setfill© | 空白を指定文字で埋める | cout « setfill('-') « setw(5) « 42; → —42 |
| 進数指定 | dec / hex / oct | 10進・16進・8進表示 | cout « hex « 255; → ff |
| 小数点桁数 | setprecision(n) | 小数点以下の桁数指定 | cout « fixed « setprecision(2) « 3.14159; → 3.14 |
| 浮動小数点表記 | fixed / scientific | 固定小数点/指数表記 | cout « scientific « 0.00123; → 1.23e-03 |
ポイント
setw() は次の出力にのみ有効(毎回指定が必要)。setfill() は継続して有効。fixed を指定しない場合、自動的に最適化される。#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
C++20以降では、Python風の書式指定が可能な std::format が導入。旧環境では fmt ライブラリ([https://github.com/fmtlib/fmt)を使用。](https://github.com/fmtlib/fmt)を使用。)
#include <format> #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
#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.
| 操作 | printf | iostream | fmt / format |
|---|---|---|---|
| 書式構文 | %d %f %s | 関数チェーン形式 | {} ベースのテンプレート式 |
| 型安全性 | × 弱い | ○ 強い | ○ 強い |
| 可読性 | △ | ○ | ◎ |
| 柔軟性 | ○ | ○ | ◎(動的組み立て可) |
<iomanip> は既存Cライクコードに馴染む標準方式。std::format / fmt は現代的で読みやすく、ゲームデバッグ出力などに最適。fmt::print を使ってデバッグメッセージを出すツール関数を作る。std::format で動的テキストを出力するプログラムを書く。