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)

第9章 文字列 と形式

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

君はこれまでC++標準ライブラリの様々な型を使い、int型やdouble型といった基本型について学んできた。 '\n'のような文字や、"Hello, world!"のようなメッセージ、さらにはstd::stringも何度か使った。しかしこれらの型の違いについてはまだ詳しく説明していない。 この章では様々な文字列型と、それらを正しく使う方法を解説する。

文字列の作成、操作、フォーマットについて学ぶ。文字列を変更しないビューであるstring_viewの作り方を示す。また、コマンドライン引数(Cスタイル文字列)をmainに渡す方法も見る。これによりプログラムの振る舞いを変えられる。

ランダムに価格を生成するコードと、ストリームから価格を読み取るコードは既に用意されている。 この章の実践的な部分では、これらの関数を使って、様々な価格に対応する改良版の取引ゲーム「Building a Trading Game」を作成する。このバージョンでは、株式の売買が可能になるだけでなく、価格をより見やすく表示できるようになる。価格を小数点以下2桁で表示し、「100」ではなく「$100.00」と表示する方法を示す。

Cスタイルの文字列リテラルと文字

main_with_args.cpp という名前の新しいファイルを作成する。このセクションのコードを試すために使う。 まず様々な文字列型について説明し、次のセクションでmain関数内での使用方法を示す。

単一文字はシングルクォート(')で表され、char型に属する。char型は文字を表現するための数値型だ。 奇妙に聞こえるかもしれないが、コンピュータは数値を扱うため、最終的には全てが数値でなければならない。 例えば'A'はASCII値65に対応する。 文字Aを宣言するには以下のいずれかを使用できる:

const char letter_from_character{'A'};
const char letter_from_number{65};

'A'を使う方が明確だが、どちらの場合もメモリ上では 65 が使われる。

二重引用符で囲まれた文字Cスタイルの文字列リテラルだ。 リテラルとは文字通りプログラムに埋め込まれた文字列のことである。 最初のプログラムの一つである例1-3のビルドコンテンツを見ると、バイナリファイルにHello, world!と書かれているのがわかるだろう。

ヒント

C++言語はC言語から派生した。 C++は「クラス付きC」として始まり、Cのスーパーセットとなった。 C++コードでCを使用することは可能だが、両言語はいくつかの点で分岐している。

文字列リテラルは個々の文字で構成される。 これらの文字には、std::vector の個々の要素と同様に、演算子[] を使ってアクセスできる。

さて、文字列Hello, world!は 13 個の文字で構成されている。 C プログラミング言語では、文字列の終わりを示すためにヌル文字\0(ASCII 値 0)を使う。したがって、このメッセージは図 9-1 に示すように 14 個の個別のchar値で構成されている。

図 9-1. 文字列リテラルを構成する個々の 文字には末尾にヌルが含まれる

メッセージをCスタイルの配列として宣言するには[]を使用できる。

const char message[]{"Hello, ...
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