當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ std::string::append()用法及代碼示例


該成員函數在字符串的末尾附加字符。

  1. 語法1:附加字符串str的字符。如果結果大小超過最大字符數,它將拋出length_error。
    string& string::append (const string& str)
    
    str: is the string to be appended.
    返回: *this
    // CPP code to demonstrate append(str) 
       
    #include <iostream> 
    #include <string> 
    using namespace std; 
       
    // Function to demonstrate append() 
    void appendDemo(string str1, string str2) 
    { 
        // Appends str2 in str1 
        str1.append(str2); 
        cout << "Using append():"; 
        cout << str1 << endl; 
    } 
       
    // Driver code 
    int main() 
    { 
        string str1("Hello World! "); 
        string str2("GeeksforGeeks"); 
       
        cout << "Original String:" << str1 << endl; 
        appendDemo(str1, str2); 
       
        return 0; 
    }

    輸出:


    Original String:Hello World! 
    Using append():Hello World! GeeksforGeeks
    
  2. 語法2:最多附加字符串str的str_num字符,從索引str_idx開始。如果str_idx> str,則拋出out_of_range。 size()。如果結果大小超過最大字符數,它將拋出length_error。
    string& string::append (const string& str, size_type str_idx, size_type str_num)
    
    str: is the string to be appended
    str_num: being number of characters
    str_idx: is index number.
    返回:*this.
    
    // CPP code to demonstrate  
    // append(const char* chars, size_type chars_len) 
      
    #include <iostream> 
    #include <string> 
    using namespace std; 
       
    // Function to demonstrate append() 
    void appendDemo(string str1, string str2) 
    { 
        // Appends 5 characters from 0th index of 
        // str2 to str1 
        str1.append(str2, 0, 5); 
        cout << "Using append():"; 
        cout << str1; 
    } 
       
    // Driver code 
    int main() 
    { 
        string str1("GeeksforGeeks "); 
        string str2("Hello World! "); 
       
        cout << "Original String:" << str1 << endl; 
        appendDemo(str1, str2); 
       
        return 0; 
    }

    輸出:

    Original String:GeeksforGeeks 
    Using append():GeeksforGeeks Hello
    
  3. 語法3:追加C-string cstr的字符。如果結果大小超過最大字符數,則拋出length_error。
    string& string::append (const char* cstr)
    
    *cstr: is the pointer to C-string.
    Note: that cstr may not be a null pointer (NULL).
    返回: *this
    
    // CPP code to demonstrate append(const char* cstr) 
      
    #include <iostream> 
    #include <string> 
    using namespace std; 
       
    // Function to demonstrate append 
    void appendDemo(string str) 
    { 
        // Appends "GeeksforGeeks" 
        // in str 
        str.append("GeeksforGeeks"); 
        cout << "Using append():"; 
        cout << str << endl; 
    } 
       
    // Driver code 
    int main() 
    { 
        string str("World of "); 
       
        cout << "Original String:" << str << endl; 
        appendDemo(str); 
       
        return 0; 
    }

    輸出:

    Original String:World of 
    Using append():World of GeeksforGeeks
    
  4. 語法4:追加字符數組chars的chars_len字符。如果結果大小超過最大字符數,則拋出length_error。
    string& string::append (const char* chars, size_type chars_len)
    
    *chars is the pointer to character array to be appended.
    chrs_len: is the number of characters from *chars to be appended.
    Note that chars must have at least chars_len characters. 
    返回: *this.
    
    // CPP code to demonstrate  
    // (const char* chars, size_type chars_len) 
      
    #include <iostream> 
    #include <string> 
    using namespace std; 
       
    // Function to demonstrate append 
    void appendDemo(string str) 
    { 
        // Appends 5 characters from "GeeksforGeeks" 
        // to str 
        str.append("GeeksforGeeks", 5); 
        cout << "Using append():"; 
        cout << str << endl; 
    } 
       
    // Driver code 
    int main() 
    { 
        string str("World of "); 
       
        cout << "Original String:" << str << endl; 
        appendDemo(str); 
       
        return 0; 
    }

    輸出:

    Original String:World of 
    Using append():World of Geeks
    
  5. 語法5:追加字符c的出現次數。如果結果大小超過最大字符數,則拋出length_error。
    string& string::append (size_type num, char c)
    
    num: is the number of occurrences
    c:is the character which is to be appended repeatedly. 
    返回: *this.
    
    // CPP code to illustrate 
    // string& string::append (size_type num, char c) 
        
    #include <iostream> 
    #include <string> 
    using namespace std; 
        
    // Function to demonstrate append 
    void appendDemo(string str) 
    { 
        // Appends 10 occurrences of '$' 
        // to str 
        str.append(10, '$'); 
        cout << "After append():"; 
        cout << str; 
       
    } 
               
    // Driver code 
    int main() 
    { 
        string str("#########"); 
       
        cout << "Original String:" << str << endl; 
        appendDemo(str); 
        
        return 0; 
    }

    輸出:

    Original String:#########
    After append():#########$$$$$$$$$$
    
  6. 語法6:追加範圍[beg,end)的所有字符。如果結果大小超過最大字符數,則拋出length_error。
    string& string::append (InputIterator beg, InputIterator end)
    
    first, last:Input iterators to the initial and final positions 
    in a sequence.
    返回 *this.
    
    // CPP code  to illustrate 
    // append (InputIterator beg, InputIterator end) 
       
    #include <iostream> 
    #include <string> 
    using namespace std; 
       
    // Function to demonstrate append 
    void appendDemo(string str1, string str2) 
    { 
       
        // Appends all characters from 
        // str2.begin()+5, str2.end() to str1 
        str1.append(str2.begin() + 5, str2.end()); 
        cout << "Using append:"; 
        cout << str1; 
    } 
    // Driver code 
    int main() 
    { 
        string str1("Hello World! "); 
        string str2("GeeksforGeeks"); 
       
        cout << "Original String:" << str1 << endl; 
        appendDemo(str1, str2); 
       
        return 0; 
    }

    輸出:

    Original String:Hello World! 
    Using append:Hello World! forGeeks
    


相關用法


注:本文由純淨天空篩選整理自 std::string::append() in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。