이 멤버 함수는 문자열 끝에 문자를 추가합니다.
- 구문 1 : 문자열 str의 문자를 추가합니다. 결과 크기가 최대 문자 수를 초과하면 length_error가 발생합니다.
string& string::append (const string& str) str : is the string to be appended. Returns : *this>
// CPP code to demonstrate append(str)> > #include> #include> 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(>'techcodeview.com'>);> > >cout <<>'Original String : '> << str1 << endl;> >appendDemo(str1, str2);> > >return> 0;> }> |
>
시합
>
산출:
Original String : Hello World! Using append() : Hello World! techcodeview.com>구문 2: 인덱스 str_idx부터 시작하여 문자열 str의 최대 str_num 문자를 추가합니다. str_idx> str이면 out_of_range가 발생합니다. 크기(). 결과 크기가 최대 문자 수를 초과하면 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. Returns : *this.>
// CPP code to demonstrate> // append(const char* chars, size_type chars_len)> > #include> #include> 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(>'techcodeview.com '>);> >string str2(>'Hello World! '>);> > >cout <<>'Original String : '> << str1 << endl;> >appendDemo(str1, str2);> > >return> 0;> }> |
>
>
산출:
Original String : techcodeview.com Using append() : techcodeview.com Hello>구문 3: C 문자열 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). Return : *this>
// CPP code to demonstrate append(const char* cstr)> > #include> #include> using> namespace> std;> > // Function to demonstrate append> void> appendDemo(string str)> {> >// Appends 'techcodeview.com'> >// in str> >str.append(>'techcodeview.com'>);> >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 techcodeview.com>구문 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. Returns : *this.>
// CPP code to demonstrate> // (const char* chars, size_type chars_len)> > #include> #include> using> namespace> std;> > // Function to demonstrate append> void> appendDemo(string str)> {> >// Appends 5 characters from 'techcodeview.com'> >// to str> >str.append(>'techcodeview.com'>, 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: 문자 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. Returns : *this.>
// CPP code to illustrate> // string& string::append (size_type num, char c)> > #include> #include> 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: [beg, end) 범위의 모든 문자를 추가합니다. 결과 크기가 최대 문자 수를 초과하면 length_error가 발생합니다.
string& string::append (InputIterator beg, InputIterator end) first, last : Input iterators to the initial and final positions in a sequence. Returns *this.>
플로피 디스크
// CPP code to illustrate> // append (InputIterator beg, InputIterator end)> > #include> #include> 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(>'techcodeview.com'>);> > >cout <<>'Original String : '> << str1 << endl;> >appendDemo(str1, str2);> > >return> 0;> }> |
>
>