0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[初級] C++: 文字列処理の基本

Last updated at Posted at 2024-05-29

C++ で使われる文字列処理の初歩の初歩
char[], std::string, CString

本記事の前提条件は以下の通りです。

  • 初心者向け
  • とは言っても、何らかのプログラムはそれなりに書けるけど、C とか C++ はちょっと、という人向け
  • ざっくり概要しか説明しないので細かいことは気にしないでいただきたい
  • Visual Studio 2013 くらい~
  • Windowsプログラム (CUI, GUI)
  • コードの検証
    • 開発環境: Visual Studio 2022, x64, Release ビルド
    • 実行環境: Windows 10
  • 本記事は上から順番に読む前提となっている
  • 「Visual Studio 2013 くらい~」と書いてあるが、本記事の内容だと現在過去未来のどのバージョンでもほぼ同じ
  • 「Windowsプログラム (CUI, GUI)」と書いてあるが、本記事の内容だとどの OS でもほぼ同じ。ただし、CString, BSTR についてはマイクロソフトによるWindows用のものなのであしからず

前提知識として必要と思われる情報

  • 本記事では、あくまでも文字列を扱う基本となる仕組みについての説明としたいので、ANSI / Unicode の違いについては触れないのであしからず。char の部分は wchar_t あるいは TCHAR と考えても問題はない

char[]

char 型データの配列であり、C / C++ で文字列を扱うための基本的な構造である。
後述の他のもの (std::string, CString) にしても、内部的には char 型(あるいは wchat_t 型かもしれないが)の配列である。
NULL を終端マーカーとし、いわゆる「NULL終端文字列」として処理する。ダブルクォートで括った文字列は、char 型の配列であり、また自動的に終端に NULL が付加される。

C言語

  • C言語では文字列と言えば通常これである
  • strcpy, strlen などの操作関数が用意されている
char szText[] = "ABC"; // sz: string terminated by zero
int len = strlen(szText);
printf("%d\n", len);
// -> 3
int size = sizeof(szText);
printf("%d\n", size);
// -> 4
for (int i = 0; i < size; i++)
{
    printf("0x%02x\n", (int)szText[i]);
}
// -> 0x41 0x42 0x43 0x00

C++

  • C++でも基本的にはこれが文字列である
  • std::strcpy, std::strlen などの操作関数が用意されている
char szText[] = "ABC";
size_t len = std::strlen(szText);
std::cout << len << "\n";
// -> 3
size_t size = sizeof(szText);
std::cout << size << "\n";
// -> 4
for (size_t i = 0; i < size; + i++)
{
    std::cout << "0x" << std::hex << std::setw(2)
        << std::setfill('0') << (int)szText[i] << "\n";
}
// -> 0x41 0x42 0x43 0x00

std::string

C++の標準ライブラリで用意された文字列クラス。
メモリ管理を内部的にやってくれるので楽、安全、というだけで、中身は結局 char 型データの配列であり、NULL終端文字列となっている。
そのため、C/C++ 標準の文字列操作関数でも扱うことができる。

std::string text = "ABC";
size_t len = text.length();
std::cout << len << "\n";
// -> 3
const char* p = text.c_str();
for (size_t i = 0; i < len + 1; i++, p++)
{
    std::cout << "0x" << std::hex << std::setw(2)
        << std::setfill('0') << (int)*p << "\n";
}
// -> 0x41 0x42 0x43 0x00

CString

マイクロソフトの文字列クラス。
MFC版、ATL版、WTL版があるが、どれも基本的には同じ。
std::string 同様、中身はNULL終端文字列となっており、C/C++ 標準の文字列操作関数でも扱うことができる。

CString text = _T("ABC");
int len = text.GetLength();
std::cout << len << "\n";
// -> 3
LPCTSTR p = (LPCTSTR)text;
for (int i = 0; i < len + 1; i++, p++)
{
    std::cout << "0x" << std::hex << std::setw(2)
        << std::setfill('0') << (int)*p << "\n";
}
// -> 0x41 0x42 0x43 0x00

BSTR

マイクロソフトのCOM / OLE / Automation 用の文字列の仕様。

少々特殊なのでまた別の機会に。

0
2
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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?