std::basic_string



basic_string

文字列クラス.正式にはSTLではないが,STLと互換性を持つ. テンプレート仕様は,

template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT> > class basic_string

文字の型,文字の特徴を記述するクラス,アロケータを指定できる.

basic_stringはvectorの文字列版のようなものであるが,コンテナの終端を表すNULL文字が定義できる, コンテナの大小関係が規定できる,などの違いがある.

C++で良く用いられるのは,

typedef basic_string<char, char_traits<char>, allocator<char> > string;
typedef basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > wstring;

の二つである.それぞれ,stringは8ビットANSI文字列,wstringは16ビットUNICODE文字列を扱う. 以下では主にstringについて説明する.

参照 http://www.cplusplus.com/reference/string/string/

インクルード

#include <string>

初期化(コンストラクタ)

string();	// 空のstring
string(const string& str);	// strで初期化
string(const string& str, size_t pos, size_t n = npos);	// strのsubstringで初期化
string(const char * s, size_t n);	// 文字配列で初期化.コピーする大きさをnで指定
string(const char * s);	// null-teminatedの文字配列で初期化
string(size_t n, char c);	// n個のcの羅列で初期化
template<class InputIterator> string (InputIterator begin, InputIterator end);	// イテレータによる初期化

要素アクセス([],at,begin,end,rbegin,rend)

  • [] : 通常の配列と同じオペレータ[]を用いたアクセス
    const char& operator[](size_t pos) const;
    char& operator[](size_t pos);
  • at() : 範囲外アクセス時にout_of_range例外を投げる関数
    const char& at(size_t pos) const;
    char& at(size_t pos);
  • begin(),end() : コンテナの最初のイテレータ,最後の次のイテレータを返す.イテレータを用いた要素アクセスに使用
    iterator begin ();
    const_iterator begin () const;
    iterator end();
    const_iterator end() const;
  • rbegin(),rend() : コンテナの最初の逆イテレータ,最後の次の逆イテレータを返す.イテレータを用いた要素逆順アクセスに使用
    reverse_iterator rend();
    const_reverse_iterator rend() const;
    reverse_iterator rend();
    const_reverse_iterator rend() const;

領域確保(resize,reserve)

  • resize(n) : コンテナのサイズをnに変更する関数.cを指定すると全ての要素がcとなる.単にnだけ指定した場合,各要素はchar()で初期化される.
    void resize(size_t n, char c);
    void resize(size_t n);
  • reserve(n) : コンテナの予約領域を確保する関数.実際に使える(アクセスできる)領域ではないが,ある程度確保したいサイズが決まっているならばpush_backする前にreserveで確保しておくと高速に処理できる.
    void reserve(size_t n=0);

サイズ確認(size,capacity,max_size,empty)

  • size() : コンテナのサイズを返す関数
    size_t size() const;
  • length() : コンテナのサイズを返す関数(size()と同じ)
    size_t length() const;
  • capacity() : コンテナの予約領域を返す関数.reserveで確保した領域サイズ.
    size_t capacity() const;
  • max_size() : コンテナが確保可能な最大サイズを返す関数.予約領域ではない.
    size_t max_size() const;
  • empty() : コンテナが空だったらtrueを返す関数.size == 0かどうかを確かめることと同義であるが,size()へのアクセスよりも高速.
    bool empty () const;

編集(+=,append,push_back,assign,insert,erase,swap,replace,copy)

  • operator+= : コンテナの最後に要素を追加
    string& operator+=(const string& str);
    string& operator+=(const char* s);
    string& operator+=(char c);
  • append : コンテナの最後に要素を追加
    string& append(const string& str);
    string& append(const string& str, size_t pos, size_t n);
    string& append(const char* s, size_t n);
    string& append(const char* s);
    string& append(size_t n, char c);
    template <class InputIterator> string& append(InputIterator first, InputIterator last);
  • push_back(val) : コンテナの最後に要素を追加
    void push_back(char c);
  • assign(n, val) : 新しい文字列を割り当てる
    string& assign(const string& str);
    string& assign(const string& str, size_t pos, size_t n);
    string& assign(const char* s, size_t n);
    string& assign(const char* s);
    string& assign(size_t n, char c);
    template <class InputIterator> string& assign(InputIterator first, InputIterator last);
  • insert() : 示された位置に要素を挿入する.
    string& insert(size_t pos1, const string& str);
    string& insert(size_t pos1, const string& str, size_t pos2, size_t n);
    string& insert(size_t pos1, const char* s, size_t n);
    string& insert(size_t pos1, const char* s);
    string& insert(size_t pos1, size_t n, char c);
    iterator insert(iterator p, char c);
    void insert(iterator p, size_t n, char c);
    template<class InputIterator> void insert(iterator p, InputIterator first, InputIterator last);
  • erase(iter) : 示された位置の要素を削除する.
    string& erase(size_t pos = 0, size_t n = npos);
    iterator erase(iterator position);
    iterator erase(iterator first, iterator last);
  • swap : 2つのコンテナのスワップ.
    void swap(string& str);
  • replace : コンテナ要素の置き換え
    string& replace(size_t pos1, size_t n1,   const string& str);
    string& replace(iterator i1, iterator i2, const string& str);
    string& replace(size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2);
    string& replace(size_t pos1, size_t n1,   const char* s, size_t n2);
    string& replace(iterator i1, iterator i2, const char* s, size_t n2);
    string& replace(size_t pos1, size_t n1,   const char* s);
    string& replace(iterator i1, iterator i2, const char* s);
    string& replace(size_t pos1, size_t n1,   size_t n2, char c);
    string& replace(iterator i1, iterator i2, size_t n2, char c);
    template<class InputIterator> string& replace(iterator i1, iterator i2, InputIterator j1, InputIterator j2);
  • copy
    size_t copy(char* s, size_t n, size_t pos = 0) const;
    呼び出し元のstringのposからn文字分をsで示される文字列にコピーする.コピーされた文字列はnull-terminatedではないので注意.
  • get_allocator
    allocator<char> get_allocator() const;
    アロケータを返す.

C文字列との互換性(c_str,data)

  • c_str
    const char* c_str() const;
    C文字列型(null-terminated)を生成して返す.
  • data
    const char* data() const;
    文字列の最初の文字を示すポインタを返す.

検索(find,find_first_of,find_first_not_of,rfind,find_last_of,find_last_not_of)

  • find, find_first_of, find_first_not_of
    size_t find(const string& str, size_t pos = 0) const;
    size_t find(const char* s, size_t pos, size_t n) const;
    size_t find(const char* s, size_t pos = 0) const;
    size_t find(char c, size_t pos = 0) const;
    string変数内の文字列str,s,文字cを検索し,その最初の位置を返す. 見つからなければ,string::nposを返す. posは検索開始位置(posの位置も検索に含む),nは検索文字数.
    size_t find_first_of(const string& str, size_t pos = 0) const;
    size_t find_first_of(const char* s, size_t pos, size_t n) const;
    size_t find_first_of(const char* s, size_t pos = 0) const;
    size_t find_first_of(char c, size_t pos = 0) const;
    パラメータはfindと同じだが,find_first_ofは文字列str,s,文字c内の任意の文字列に 一致するものを検索する.
    size_t find_first_not_of(const string& str, size_t pos = 0) const;
    size_t find_first_not_of(const char* s, size_t pos, size_t n) const;
    size_t find_first_not_of(const char* s, size_t pos = 0) const;
    size_t find_first_not_of(char c, size_t pos = 0) const;
    文字列str,sもしくは文字c内の任意の文字列に一致しない最初の位置を返す.
  • rfind, find_last_of, find_last_not_of
    size_t rfind(const string& str, size_t pos = npos) const;
    size_t rfind(const char* s, size_t pos, size_t n) const;
    size_t rfind(const char* s, size_t pos = npos) const;
    size_t rfind(char c, size_t pos = npos) const;
    string変数内の文字列str,s,文字cを検索し,その最後の位置を返す. 見つからなければ,string::nposを返す. posは検索終了位置(posの位置も検索に含む),nは検索文字数.
    size_t find_last_of(const string& str, size_t pos = npos) const;
    size_t find_last_of(const char* s, size_t pos, size_t n) const;
    size_t find_last_of(const char* s, size_t pos = npos) const;
    size_t find_last_of(char c, size_t pos = npos) const;
    パラメータはrfindと同じだが,find_last_ofは文字列str,s,文字c内の任意の文字列に 一致するものを検索する.
    size_t find_last_not_of(const string& str, size_t pos = npos) const;
    size_t find_last_not_of(const char* s, size_t pos, size_t n) const;
    size_t find_last_not_of(const char* s, size_t pos = npos) const;
    size_t find_last_not_of(char c, size_t pos = npos) const;
    文字列str,sもしくは文字c内の任意の文字列に一致しない最後の位置を返す.

部分文字列(substr)

  • substr
    string substr(size_t pos = 0, size_t n = npos) const;
    posからn文字分の部分文字列を返す.posから最後までの文字列の場合はnにnposを渡す.

比較(compare)

  • compare
    int compare(const string& str) const;
    int compare(const char* s) const;
    int compare(size_t pos1, size_t n1, const string& str) const;
    int compare(size_t pos1, size_t n1, const char* s) const;
    int compare(size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2) const;
    int compare(size_t pos1, size_t n1, const char* s, size_t n2) const;
    他の文字列との比較.比較する文字列が全て一致していれば0, 一致しない最初の文字を比較して,辞書順で元の文字列<比較文字列なら-1,その逆なら1を返す.
    使用例
    int main(void)
    {
    	string s0 = "abcdefg";
    	string s1 = "abcdffg";
    	cout << s0 << " - " << s1 << " : " << s0.compare(s1) << endl;
    
    	s1 = "abcdeeg";
    	cout << s0 << " - " << s1 << " : " << s0.compare(s1) << endl;
    
    	return 0;
    }
    結果は,
    abcdefg - abcdffg : -1
    abcdefg - abcdeeg : 1

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2024-03-08 (金) 18:06:08