C ++文字列クラスの概要

1. C ++の文字列

C++ 语言中有单独字符串类型,在string头文件中,还有对应的宽字符串wstring.

string iStr =“ Hello world”;
wstring wStr =“ Hello world”;

2. VC ++の文字列

ラーニングウィンドウは、一連の文字列マクロよりも開発プロセスの中で最も厄介な部分です。
タイプMBCSはUnicodeを意味します
TCHARを意味します
charwchar_t WCHAR wchar _twchar_t
LPSTR char * char *
LPCWSTR const wchar_t * const wchar_t *
LPCSTR const char * const char *
LPWSTR wchar_t * wchar_t *
LPTSTR TCHAR * TCHAR *
LPCTSTR const TCHAR * const TCHAR *
char文字標準のタイプc(1Byte)
wchar_t UNICODE文字セットのタイプ(2Byte)
TCHARは次のように定義されます。

#ifdef UNICODE  
typedef wchar_t TCHAR;  
#else  
typedef char TCHAR;  
#endif  

//再了解一个宏_T(),使用宏_T(),使代码有了unicode的意识。
#ifdef UNICODE  
#define _T(x) L##x  
#else  
#define _T(x) x  

3.標準のC ++文字列操作

3.1工法

string(const char * s); //文字列を初期化sc
string(int n、char c) ; // n文字で初期化c
さらに、stringクラスは、strings1; Stringなどのデフォルトのコンストラクターとコピーコンストラクターもサポートします。 s2 =” hello”;は正しい書き方です。構築された文字列が長すぎて表現できない場合、length_error例外がスローされます。3.2
操作方法

3.2.1文字操作

const char&operator [](int n)const; //インデックス操作、1文字を取る
const char&at(int n)const; //上記と同じ
char&operator [](int n); //上記と同じ
char&at (int n); //上記と同じ
operator []とat()は両方とも、現在の文字列のn番目の文字の位置を返しますが、at関数は範囲チェックを提供し、範囲外の場合はout_of_range例外がスローされます。添字演算子[]はチェックアクセスを提供しません。

const char * data()const; //ヌルで終了していないc文字配列を
返しますconstchar * c_str()const; //ヌルで終了しているc文字列を返しますintcopy
(char * s、int n、int pos = 0)const; //現在の文字列のposで始まるn文字をsで始まる文字配列にコピーし、実際のコピー数を返し
ます3.2.2文字列の特徴的な説明

intcapacity()const; //現在の容量(つまり、メモリを増やさずに文字列に格納できる要素の数
)を返しますint max_size()const; //可能な最大文字列の長さを返します文字列オブジェクトに格納
intsize()const; //現在の文字列のサイズを返しますintlength()const; //現在の文字列
の長さを返します
boolempty()const; //現在の文字列が空かどうか
void resize(int len、char c); //文字列の現在のサイズをlenに設定し、不十分な部分を文字cで埋めます

3.2.3文字列クラスの入出力操作

文字列クラスのオーバーロードされた演算子演算子>>は入力に使用され、オーバーロードされた演算子演算子<<は出力操作に使用されます。
関数getline(istream&in、string&s);は、入力ストリームからsへの文字列を改行文字「\ n」で区切って読み取るために使用されます。
3.2.4文字列の割り当て

string&operator =(const string&s); //文字列sを現在の文字
列に割り当てますstring&assign(const char * s); // c型の文字
を割り当てますstrings&assign(const char * s、int n); //割り当てますc文字列で始まるn文字
文字列&assign(const string&s); //文字列sを現在の文字
文字列に割り当てる&assign(int n、char c); // n文字で割り当てるc現在の文字
文字列を指定&assign(const string&s 、int start、int n); //文字列sの開始から現在の文字
文字列にn文字を割り当てます&assign(const_iterator first、const_itertor last); // put最初と最後のイテレータの間の部分が文字列に割り当てられます
3.2.5文字列接続

string&operator + =(const string&s); //文字列sを現在の文字
列の末尾に接続しますstring&append(const char * s); // cタイプの文字列sを現在の文字
列の末尾に接続しますstring&append(const char * s、int n); // cタイプの文字列sの最初のn文字を現在の文字
文字列の末尾に接続します&append(const string&s); // operator + =()
string&append(const string&sと同じ)、int pos、int n); //文字列sのposから始まるn文字を現在の文字
列stringの末尾に接続します&append(int n、char c); //現在の文字列の末尾にn文字を追加しますc
string&append(const_iterator first、const_iterator last); //イテレーターの最初と最後の間の部分を現在の文字列の最後に接続します
3.2.6文字列の比較

bool operator ==(const string&s1 、const string&s2)const; // 2つの文字列が等しい場合比較
演算子 ">"、 "<"、 "> ="、 "<="、was "=!"オーバーロードは文字列の比較に使用されます;
int compare(const string&s)const; //現在の文字列とsのサイズを
比較しますintcompare(int pos、int n、const string&s)const; //現在の文字列を比較しますposおよびsから始まるn文字で構成される文字列のサイズ
intcompare(int pos、int n、const string&s、int pos2、int n2)const; // posで始まるn文字で構成される現在の文字列を比較します。 sのpos2から始まるn2文字の文字列intcompare
(const char * s)const;
int compare(int pos、int n、const char * s)const;
int compare(int pos、int n、const char * s、 int pos2)const;
比較関数は、>の場合は1、<の場合は-1、==の場合は0を返します。3.2.7
文字列のサブストリング

string substr(int pos = 0、int n = npos)const; // pos3.2.8で始まるn文字で構成される文字列を返します
文字列交換

void swap(string&s2); //現在の文字列とs2の値を交換します
3.2.9文字列クラスの検索関数

int find(char c、int pos = 0)const; // posから現在の文字列内の文字cの位置を検索しますintfind
(const char * s、int pos = 0)const; // posから文字列を検索します現在の文字列のsの位置
intfind(const char * s、int pos、int n)const; //現在の文字列のposから文字列sの最初のn文字の位置を検索します
intfind(const string&s、 int pos = 0)const; // posから始まる現在の文字列内の文字列sの位置を検索します//
検索が成功したときに位置を返します。失敗した場合はstring :: nposの値を返します
intrfind(char c、int pos = npos)const; // posから始まる現在の文字列内の文字cの位置を検索
intrfind(const char * s、int pos = npos)const;
int rfind(const char * s、int pos、int n = npos )const;
int rfind(const string&s、int pos = NPoS)const;
//文字列から文字列の開始位置を検索pos s現在の文字列の最初のn文字を後ろから前に、正常な戻り位置、On失敗した場合は、string :: nposの値を返します
。intfind_first_of(char c、int pos = 0)const; // posから最初に出現する文字cの位置を検索します。
int find_first_of(const char * s、int pos = 0)const;
int find_first_of(const char * s、int pos、int n)const;
int find_first_of(const string&s、int pos = 0)const;
// posから開始現在の文字列のsの最初のn文字で構成される配列の最初の文字の位置を見つけます。検索に失敗するとstring :: npos
int find_first_not_of(char c、int pos = 0)const;
int find_first_not_of(const char * s、int pos = 0)const;
int find_first_not_of(const char * s、int pos、int n)const ;
int find_first_not_of(const string&s、int pos = 0)const;
//現在の文字列から文字列sにない最初の文字の位置を検索し、失敗時に戻りますstring :: npos
int find_last_of(char c、int pos = npos)const;
int find_last_of(const char * s、int pos = npos)const;
int find_last_of(const char * s、int pos、int n = npos)const;
int find_last_of(const string&s、int pos = npos)const;
int find_last_not_of(char c、int pos = npos)const;
int find_last_not_of(const char * s、int pos = npos)const;
int find_last_not_of(const char * s、 int pos、int n)const;
int find_last_not_of(const string&s、int pos = npos)const;
// find_last_ofとfind_last_not_ofは
、3.2.10文字列を後ろから検索するための置換関数であることを除いて、find_first_ofとfind_first_not_ofに似ています。フロント

string&replace(int p0、int n0、const char * s); // p0から始まるn0文字を削除し、p0に
文字列sを挿入しますstring&replace(int p0、int n0、const char * s、int n); / / p0から始まるn0文字を削除してから、
文字列sの最初のn文字をp0に挿入しますstring&replace(int p0、int n0、const string&s); // p0から始まるn0文字を削除してから、文字列s
string&replace( int p0、int n0、const string&s、int pos、int n)at p0; // p0から始まるn0文字を削除し、posから始まるn文字をp0の文字列sに挿入し
ます文字列&replace(int p0、int n0、int n、char c); // p0からn0文字を削除し、p0 cにn文字を挿入し
ますstring&replace(iterator first0、iterator last0、const char * s); // [first0、last0)の間の部分を置き換えます文字列
sstring&replace(iterator first0、iterator last0、const char * s、int n); // [first0、last0)の間の部分をs
文字列の最初のn文字に置き換えます&replace(iterator first0、iterator last0、const string&s); // [first0、last0)の間の部分をstringsに置き換えます
string&replace(iterator first0、iterator last0、int n、char c); // [first0、last0)の間の部分をn文字に
置き換えますc string&replace(iterator first0、iterator last0、const_iterator first、const_iterator last); //置換[first0、last0)と[first、last)の間の文字列
3.2.11文字列クラスの挿入関数

string&insert(int p0、const char * s);
string&insert(int p0、const char * s、int n);
string&insert(int p0、const string&s);
string&insert(int p0、const string&s、int pos 、int n);
//最初の4つの関数は、位置p0の
文字列sのposから始まる最初のn文字を挿入しますstring&insert(int p0、int n、char c); //この関数はp0c
イテレーターにn文字を挿入しますinsert(iterator it、char c); //文字cを挿入し、挿入後に
イテレーターの位置を返しますvoid insert(iterator it、const_iterator first、const_iterator last); // Insert [first、last)
Void insert(iterator it、int n、char c); //それにn文字を挿入
c3.2.12文字列クラスの関数を削除

イテレータerase(iterator first、iterator last); // [first、last)の間のすべての文字を削除し、削除後に
イテレータの位置を返しますiteratorerase(iterator it); //それが指す文字を削除し、イテレータに戻ります削除後
文字列の位置&erase(int pos = 0、int n = npos); // posの先頭にあるn文字を削除し、変更された文字列を返します
3.2.13文字列クラスのイテレータ処理

文字列クラスは、順方向および逆方向のトラバーサル用のイテレータを提供します。イテレータは、ポインタ操作と同様に、個々の文字にアクセスするための構文を提供し、イテレータは範囲をチェックしません。
string :: iteratorまたはstring :: const_iteratorを使用して、イテレータ変数を宣言します。const_iteratorは、反復の内容を変更することはできません。一般的なイテレータ関数は次の
とおりです。const_iteratorbegin()const;
iterator begin(); //文字列の開始位置を返します
const_iteratorend()const;
iterator end(); //文字列の最後の文字の後の位置を返します
const_iteratorrbegin ()const;
iterator rbegin(); //文字列の最後の文字の位置を返します
const_iteratorrend()const;
iterator rend(); //文字列の最初の文字の前を返します
rbeginとrendが使用されます後ろから前へ反復アクセスは、イテレータstring :: reverse_iterator、string :: const_reverse_iteratorを設定することによって実現されます。

3.2.14文字列ストリーム処理

Ostringstreamは、変数とistringstream、ヘッダーファイルを定義することによって実現されます
。次に例を示します。

string input("hello,this is a test");  
    istringstream is(input);  
    string s1,s2,s3,s4;  
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"  
    ostringstream os;  
    os<<s1<<s2<<s3<<s4;  
    cout<<os.str();  

3.2.15文字列セグメンテーション機能

vector<string> split(string& str,const char* c)  
{  
    char * p;  
    vector<string> res;  
    p = strtok(const_cast<char*>(str.c_str()),c);  
    while(p!=NULL)  
    {  
        res.push_back(p);  
        p = strtok(NULL,c);  
    }  
    return res;   
}  

3.2.16文字列置換機能

void Replace(std::string&srcstr,const std::string&oldstr,const std::string&newstr)  
    {  
        string::size_type pos=0;  
        string::size_type a=oldstr.size();  
        string::size_type b=newstr.size();  
        while((pos=srcstr.find(oldstr,pos))!=string::npos)  
        {  
            srcstr.replace(pos,a,newstr);  
            pos+=b;  
        }  
    }  

3.2.17文字列のトリムメソッド

std::string TrimLeft(const std::string& str) {  
        std::string t = str;  
        for (std::string::iterator i = t.begin(); i != t.end(); i++) {  
            if (!isspace(*i)) {  
                t.erase(t.begin(), i);  
                break;  
            }  
        }  
        return t;  
    }  

    std::string TrimRight(const std::string& str) {  
        if (str.begin() == str.end()) {  
            return str;  
        }  
        std::string t = str;  
        for (std::string::iterator i = t.end() - 1; i != t.begin(); i--) {  
            if (!isspace(*i)) {  
                t.erase(i + 1, t.end());  
                break;  
            }  
        }  
        return t;  
    }  

    std::string Trim(const std::string& str) {  
        std::string t = str;  

        std::string::iterator i;  
        for (i = t.begin(); i != t.end(); i++) {  
            if (!isspace(*i)) {  
                t.erase(t.begin(), i);  
                break;  
            }  
        }  
        if (i == t.end()) {  
            return t;  
        }  

        for (i = t.end() - 1; i != t.begin(); i--) {  
            if (!isspace(*i)) {  
                t.erase(i + 1, t.end());  
                break;  
            }  
        }  
        return t;  
    }  

4文字列変換

4.1 string、char *、const char *相互変換

char * s = "hello world";  
    const char * cs = "world";  
    string str;  
    str = s; // char* -> string  
    s = const_cast<char*>(cs); // const char* -> char*  
    cs = s;// char* -> const char*  
    s = const_cast<char*>(str.c_str());// string -> const char* -> char*  

4.1シェーピングと文字列変換

// unsigned int to string 。用以取代atoi  
static inline string Int2Str(unsigned int value)  
{  
    string result;  
    stringstream buffer;  
    buffer << std::dec << value;     
    buffer >> result;  
    return result;  
}  

// string to unsigned int  
static inline unsigned int Str2Int(string value)  
{  
    unsigned int result;  
    stringstream buffer;  
    buffer << std::dec << value;   
    buffer >> result;    
    return result;  
}  

4.116進数と文字列の相互変換

// unsigned int to hex string  
static inline string Uint2StrHex(unsigned int value)  
{  
    string result;  
    stringstream buffer;  
    buffer << hex << value;     
    buffer >> result;  
    return result;  
}  

5参考資料
http://www.cnblogs.com/fdyang/archive/2012/06/01/2858741.html

おすすめ

転載: blog.csdn.net/yjh_SE007/article/details/78427562