GeezerVison is showing cpprefjp.github.io — std::basic_string - cpprefjp C++日本語リファレンス
最終更新日時:
2026年08月27日 14時02分48秒 (JST)
Akira Takahashi
が更新
履歴
編集
class template
<string>
std:: basic_string
namespace std {
template < class charT ,
class traits = char_traits < charT > ,
class Allocator = allocator < charT > >
class basic_string ;
using string = basic_string < char > ;
using u8string = basic_string < char8_t > ; // C++20から
using u16string = basic_string < char16_t > ; // C++11から
using u32string = basic_string < char32_t > ; // C++11から
using wstring = basic_string < wchar_t > ;
namespace pmr { // C++17から
template < class charT , class traits = char_traits < charT >>
using basic_string =
std::basic_string < charT , traits , polymorphic_allocator < charT >> ;
using string = basic_string < char > ;
using u8string = basic_string < char8_t > ; // C++20から
using u16string = basic_string < char16_t > ;
using u32string = basic_string < char32_t > ;
using wstring = basic_string < wchar_t > ;
}
}
概要
basic_stringクラスは、あらゆる文字型を使用できる文字列クラスである。
テンプレートパラメータとして文字型を受け取るようになっており、使用を容易にするため、以下のパラメータ設定済みエイリアスが定義されている。
エイリアス
説明
対応バージョン
string
char型文字列。ASCII、UTF-8等のマルチバイト文字列や、バイト配列として使用する。
wstring
wchar_t型文字列。wchar_tが16ビットの環境で、UTF-16の文字列として使用する。
u8string
char8_t型文字列。UTF-8の文字列として使用する。
C++20
u16string
char16_t型文字列。UTF-16の文字列として使用する。
C++11
u32string
char32_t型文字列。UTF-32の文字列として使用する。
C++11
pmr::string
多相アロケータ を用いるstring
C++17
pmr::wstring
多相アロケータ を用いるwstring
C++17
pmr::u8string
多相アロケータ を用いるu8string
C++20
pmr::u16string
多相アロケータ を用いるu16string
C++17
pmr::u32string
多相アロケータ を用いるu32string
C++17
basic_string文字列オブジェクトに含まれる各要素は、必ずしも1文字を表すわけではないことに注意が必要である。
このクラスが表すのは、文字型charTの動的配列であり、文字の動的配列ではない。
したがって、文字列中に以下のようなものが含まれている場合、basic_stringクラスにおいては複数の要素と見なされる。
マルチバイト文字(charTがcharなどの場合)
サロゲートペア
結合文字列
異体字切り換えシーケンス (IVS)
適格要件
C++17 : traits::char_typeがcharTと同じ型であること
C++98からC++14までは、要件ではなく注記として述べられていた
例外安全性
basic_stringのメンバ関数および演算子が例外を送出した場合、そのメンバ関数・演算子は何の効果も持たない。また、erase() とpop_back() は例外を送出しない。
メンバ関数
構築・破棄
イテレータ
名前
説明
対応バージョン
begin
先頭の要素を指すイテレータを取得する
end
末尾の次を指すイテレータを取得する
cbegin
先頭の要素を指す読み取り専用イテレータを取得する
C++11
cend
末尾の次を指す読み取り専用イテレータを取得する
C++11
rbegin
末尾を指す逆イテレータを取得する
rend
先頭の前を指す逆イテレータを取得する
crbegin
末尾を指す読み取り専用逆イテレータを取得する
C++11
crend
先頭の前を指す読み取り専用逆イテレータを取得する
C++11
領域
要素アクセス
文字列の変更
文字列の操作
メンバ定数
名前
説明
対応バージョン
npos
無効な位置を表す。findやsubstrなどで使われる。static const size_type npos = -1;
C++23からconstexpr
メンバ型
名前
説明
対応バージョン
traits_type
文字特性型 traits
value_type
文字型 C++98 : traits::char_type C++17 : charT
allocator_type
アロケータ型 Allocator
size_type
要素数を表す符号なし整数型。 allocator_traits<Allocator>::size_type
difference_type
イテレータの差を表す符号付き整数型。 allocator_traits<Allocator>::difference_type
reference
参照型 value_type&
const_reference
const参照型 const value&
pointer
ポインタ型 allocator_traits<Allocator>::pointer
const_pointer
constポインタ型 allocator_traits<Allocator>::const_pointer
iterator
イテレータ
const_iterator
読み取り専用イテレータ
reverse_iterator
逆順イテレータ reverse_iterator <iterator>
const_reverse_iterator
読み取り専用逆イテレータ reverse_iterator <const_iterator>
非メンバ関数
文字列連結
比較演算子
入出力
入れ替え
名前
説明
対応バージョン
swap
2つのbasic_stringオブジェクトを入れ替える
要素削除
名前
説明
対応バージョン
erase
指定した値をもつ要素とその分の領域を、コンテナから削除する
C++20
erase_if
指定した条件に合致する要素とその分の領域を、コンテナから削除する
C++20
リテラル
名前
説明
対応バージョン
s
basic_stringのリテラル
C++14
推論補助
ハッシュサポート
名前
説明
対応バージョン
hash
hashクラスの特殊化
C++11
例
基本的な使い方 (C++98)
#include <iostream>
#include <cstdio>
#include <string>
int main ()
{
// C文字列からstringオブジェクトを構築
std::string s = "hello" ;
// 末尾に文字列を追加
s += " world" ;
// 部分文字列を取得(始点:0、始点からの文字数:5)
std::string hello = s.substr ( 0 , 5 );
// ostreamへの出力
std::cout << hello << std::endl ;
// C文字列を取得し、const char*を要求するAPIに渡す
std :: printf ( "%s" , s.c_str() );
}
出力
hello
hello world
定数式内でbasic_stringを使用する (C++20)
#include <cassert>
#include <string>
constexpr bool f ()
{
std::string s = "Hello" ;
s += " World" ;
auto * p = s.data() ;
assert ( p );
return true ;
}
int main ()
{
static_assert ( f ());
}
参照