博客專欄

        EEPW首頁 > 博客 > c++ string詳解 assign

        c++ string詳解 assign

        發布人:電子禪石 時間:2021-09-17 來源:工程師 發布文章

        c++ string詳解 assign - baraka - 博客園 (cnblogs.com)



        assign方法可以理解為先將原字符串清空,然后賦予新的值作替換。

        返回類型為 string類型的引用。其常用的重載也有下列幾種:

         

        a. string& assign ( const string& str );

        將str替換原字串的內容

        舉例:

        string testassign = "Hello World";

        testassign.assign("Go home");

        cout<<testassign<<endl;

        //打印結果為 go home

         

        b. string& assign ( const string& str, size_t pos, size_t n );

        將str的內容從位置pos起的n個字符作為原字串的新內容賦給原字串

        string testassign = "Hello World";

        testassign.assign("Come on!", 5, 2);

        cout<<testassign<<endl;

        //打印結果為 on

         

        c. string& assign ( const char* s, size_t n );

        將字符數組或者字符串的首n個字符替換原字符串內容

        舉例:

        string testassign = "Hello World";

        testassign.assign("go back to China", 7);

        cout<<testassign<<endl;

        //打印結果為go back

         

        d. string& assign ( const char* s );

        將字符串或者字符數組作為新內容替換原字串

        舉例:

        string testassign = "Hello World";

        char ch[20] = "go back to shanghai";

        testassign.assign(ch);

        cout<<testassign<<endl;

        //打印結果為 go back to shanghai

         

        e. string& assign ( size_t n, char c );

        將原字串替換為n個字符c

        舉例:

        string testassign = "Hello World";

        char ch = '?';

        testassign.assign(5, ch);

        cout<<testassign<<endl;

        //打印結果為?????

         

        f. template <class InputIterator>   string& assign ( InputIterator first, InputIterator last );

        需要include <iterator>

        舉例:

        string testassign = "Hello World";

        testassign.assign(istream_iterator<char>(cin), istream_iterator<char>());

        //輸入abcde

        cout<<testassign<<endl;

        //打印結果為 abcde

         

        ---------------------------------------------------------------------------------------

        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 );
        Assign content to string

        Assigns new content to the string replacing its current content.

        The arguments passed to the function determine the new content:

        • string& assign ( const string& str );

        • Sets a copy of str as the new content.

        • string& assign ( const string& str, size_t pos, size_t n );

        • Sets a copy of a substring of str as the new content. The substring is the portion of str that begins at the character position pos and takes up to n characters (it takes less than n if the end of str is reached before).

        • string& assign ( const char * s, size_t n );

        • Sets as the new content a copy of the string formed by the first n characters of the array pointed by s.

        • string& assign ( const char * s );

        • Sets a copy of the string formed by the null-terminated character sequence (C string) pointed by s as the new content. The length of the character sequence is determined by the first ocurrence of a null character (as determined by traits.length(s)).

        • string& assign ( size_t n, char c );

        • Sets a string formed by a repetition of character cn times, as the new content.

        • template<class InputIterator> string& assign (InputIterator first, InputIterator last);

        • If InputIterator is an integral type, behaves as the previous member function version, effectively setting as the new content a string formed by the repetition first times of the character equivalent to last.
          In any other case, the content is set to the values of the elements that go from element referred to by iterator first to the element right before the one referred to by iterator last.


         

        Parameters

        • str

        • Another object of class string whose content is entirely or partially copied as the new content for the string.

        • pos

        • Starting position of the substring of the string object str that forms the new content. Notice that the first position has a value of 0, not 1.
          If the position passed is past the end of str, an out_of_range exception is thrown.

        • n

        • Number of characters to use for the content (i.e., its length).

        • s

        • Array with a sequence of characters.
          In the third member function version, the length is determined by parameter n, even including null characters in the content.
          By contrast, in the fourth member version, s is a null-terminated character, and therefore its length is determined only by the first occurrence of a null character.

        • c

        • Character value to be repeated n times to form the new content.

        • start

        • If along with last, both are integers, it is equivalent to parameter n, otherwise it is an iterator referring to the beginning of a sequence of characters.

        • last

        • If along with start, both are integers, it is equivalent to parameter c, otherwise it is an iterator referring to the past-the-end element of a sequence of characters.


         

        Return Value

        *this

        Example

        1234567891011121314151617181920212223242526272829303132333435
        // string::assign#include <iostream>#include <string>usingnamespacestd;int main (){  string str;  string base="The quick brown fox jumps over a lazy dog.";// used in the same order as described above:str.assign(base);  cout << str << endl;
        
          str.assign(base,10,9);  cout << str << endl;// "brown fox"str.assign("pangrams are cool",7);  cout << str << endl;// "pangram"str.assign("c-string");  cout << str << endl;// "c-string"str.assign(10,'*');  cout << str << endl;// "**********"str.assign<int>(10,0x2D);  cout << str << endl;// "----------"str.assign(base.begin()+16,base.end()-12);  cout << str << endl;// "fox jumps over"return0;
        }

         


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞: C

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 云霄县| 郓城县| 荣成市| 黄梅县| 浮山县| 泽州县| 临朐县| 四会市| 玉田县| 连云港市| 邛崃市| 富川| 新晃| 澎湖县| 太康县| 盐津县| 伊通| 中西区| 吉木萨尔县| 延长县| 蒲城县| 兴业县| 甘孜县| 车险| 郓城县| 呼伦贝尔市| 安丘市| 南皮县| 合山市| 邢台市| 五华县| 外汇| 长丰县| 子洲县| 太谷县| 潮安县| 泾川县| 科技| 密云县| 图们市| 贡嘎县|