C++STL Stringクラス(2)

5772 ワード

C++STL Stringクラス(2)
string内容の修正
一般的にstringを修正する方法はたくさんあります.例えば、assign()、operator=、erase()、swap()、insert()、append()などです.
assignこの関数はstringに直接再割り当てできます.たとえば、次のようにします.
#include 
#include 
using namespace std;
int main(){
    string str1 ("123456");
    string str;
    str.assign(str1);  //    assign  str1   str
    cout<

出力:
123456
456
3456
12345

erase()消去関数は、string上の要素を削除します.
str.erase (str* begin(), str.end());
str.erase (3);
str.swap (str2);

Insert()挿入関数
basic_string& insert (size_type p0 , const E * s); //   1         s   
basic_string& insert (size_type p0 , const E * s, size_type n); //   s    3      p0   
basic_string& insert (size_type p0, const basic_string& str);
basic_string& insert (size_type p0, const basic_string& str,size_type pos, size_type n); //   str    
basic_string& insert (size_type p0, size_type n, E c); //    p0       n     c
iterator insert (iterator it, E c); //  it        c
void insert (iterator it, const_iterator first, const_iterator last); //         
void insert (iterator it, size_type n, E c) ; //  it        n     c

例:
string A ("ello");
string B;
B.insert(1,A);
cout<

find()、rfind()find()関数および他の関数が所望の文字(またはサブストリング)を検索しない場合はnposを返し、検索に成功すると、検索した1番目の文字またはサブストリングの位置を返す.nposは符号なし整数値であり、初期値は-1である.検索に失敗すると、nposは「見つからない(not found)」または「残りのすべての文字」を表す. ここでfind()は順序検索であり、rfind()は逆順序検索である.
size_type find (value_type _Chr, size_type _Off = 0) const;
//find()    1          、 2        **         **
size_type find (const value_type* _Ptr , size_type _Off = 0) const;
//find()    1           , 2                 
size_type find (const value_type* _Ptr, size_type _Off = 0, size_type _Count) const;
// 1           , 2              , 3       1        ,    _Ptr       ,     _Ptr        
size_type find (const basic_string& _Str, size_type _Off = 0) const;
// 1           , 2                

find_first_of()、 find_last_of()この2つの関数は主にソース文字列で文字列を検索する機能を実現し、その中で見つかった最初の文字列のindexを返します.クエリに失敗するとnposが返されます.ここでfind_first_ofは順方向、find_last_ofは逆方向、つまり最後のマッチングです.
size_type find_first_not_of (value_type_Ch, size_type_Off = 0) const; size_type find_first_of (const value_type* _Ptr, size_type _Off = 0) const;
size_type find_first_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_of (const basic_string & _Str, size_type_Off = 0) const;
size_type find_last_of (value_type _Ch, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type_Off = npos) const;
size_type find_last_of (const value_type* _Ptr, size_type _Off, size_type _Count) const;
size_type find_last_of (const basic_string& _Str, size_type_Off = npos) const;

find_first_not_of()、find_last_not_of()この2つの関数は、検索された文字列とターゲット文字が一致しない最初の文字、または最後の文字です.
size_type find_first_not_of (value_type _Ch, size_type_Off = 0) const;
size_type find_first_not_of (const value_type * _Ptr, size_type_Off = 0) const;
size_type find_first_not_of (const value_type* _Ptr, size_type_Off, size_type_Count) const;
size_type find_first_not_of (const basic_string & _Str, size_type_Off = 0) const;

C++stringサポート反復器
反復器はSTLの精髄であり、STLには全部で5種類の反復器があり、それぞれ入力反復器、出力反復器、順方向反復器、双方向反復器及びランダム反復器である.ここでstd::stringは従来の反復器と逆反復器を実現している.ここで提供される具体的な関数は主にbegin()end()rbegin()rend()append()assign()insert()erase()replace()などである.
#include 
#include 
#include 
using namespace std;
int main ()
{
    string s ("The zip code of Hondelage in Germany is 38108.");
    cout << "Original: " << s << endl;
    string sd(s.begin(),s.end ()); //          
    cout << "Destination: " << sd << endl;
    transform (sd.begin(), sd.end(), sd.begin(), toupper); //        (   )
    cout << "Destination (All Toupper)): " << sd << endl;
    string sd1;
    sd1.append (sd.begin(),(sd.end() -7)); //append()        
    cout << "Destination sd1: " << sd1 << endl;
    string sd2;
    string::reverse_iterator iterA;
    string temp = "0";
    for (iterA = sd.rbegin (); iterA != sd.rend (); iterA++) //reverse_iterator
    {
        temp=* iterA;
        sd2.append (temp);
    }
    cout << "Destination sd2: " << sd2 << endl;
    sd2.erase (0, 15); //erase()        
    cout << "Destination sd2 (Erased 15 chars) : " << sd2 << endl;
    string::iterator iterB = sd2.begin ();
    string sd3 = string ("12345678");
    sd2.insert (sd2.begin(), sd3.begin(), sd3.end()); //insert()        
    cout << "Destination sd2 (Insert 8 chars) : " << sd2 << endl;
    sd2.replace (sd2.begin (), sd2.end(), "This is an Exarrple of Replace"); //Replace
    cout <

対応する出力は次のとおりです.
Original: The zip code of Hondelage in Germany is 38108.
Destination: The zip code of Hondelage in Germany is 38108.
Destination (All Toupper)): THE ZIP CODE OF HONDELAGE IN GERMANY IS 38108.
Destination sd1: THE ZIP CODE OF HONDELAGE IN GERMANY IS
Destination sd2: .80183 SI YNAMREG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Erased 15 chars) : EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Insert 8 chars) : 12345678EG NI EGALEDNOH FO EDOC PIZ EHT
Destination sd2 (Replace All): This is an Exarrple of Replace