首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_string::append

basic_string& append( size_type count, CharT ch );

(1)

basic_string& append( const basic_string& str );

(2)

(3)

basic_string& append( const basic_string& str, size_type pos, size_type count );

(until C++14)

basic_string& append( const basic_string& str, size_type pos, size_type count = npos );

(since C++14)

basic_string& append( const CharT* s, size_type count );

(4)

basic_string& append( const CharT* s );

(5)

template< class InputIt > basic_string& append( InputIt first, InputIt last );

(6)

basic_string& append( std::initializer_list<CharT> ilist );

(7)

(since C++11)

basic_string& append( std::basic_string_view<CharT, Traits> sv );

(8)

(since C++17)

template < class T > basic_string& append( const T& t, size_type pos, size_type count = npos );

(9)

(since C++17)

在字符串中追加其他字符。

1%29附件count字符副本ch

2%29附加字符串str

3%29追加一个子字符串[pos, pos+count)str如果请求的子字符串持续到字符串的末尾,或者count == npos,附加的子字符串是[pos, size()).如果pos >= str.size(),,,std::out_of_range被扔了。

4%29附录一count所指向的字符串字符s...s可以包含空字符。

5%29附加以空结尾的字符串。s字符串的长度由第一个空字符决定。

6%29个附加字符在范围内[first, last)。此重载与重载%281%29的效果相同,如果InputIt是一个整体类型。

7%29追加初始化程序列表中的字符ilist...

8%29追加字符串视图中的所有字符sv好像append(sv.data(), sv.size())

9%29名皈依者t到字符串视图sv好像std::basic_string_view<charT, traits> sv = t;,然后从子视图中追加字符。[pos, pos+count)sv.如果所请求的子视图扩展到sv,或者如果count == npos,附加的子视图是[pos, sv.size()).如果pos >= sv.size(),,,std::out_of_range被扔了。此重载只参与在下列情况下的重载解决方案:std::is_convertible_v<const T&,std::basic_string_view<CharT, Traits>>truestd::is_convertible_v<const T&, const CharT*>false...

参数

count

-

number of characters to append

pos

-

the index of the first character to append

ch

-

character value to append

first, last

-

range of characters to append

str

-

string to append

s

-

pointer to the character string to append

ilist

-

initializer list with the characters to append

sv

-

std::basic_string_view with the characters to append

t

-

object convertible to std::basic_string_view with the characters to append

返回值

*this...

复杂性

没有标准的复杂性保证,典型的实现行为类似于STD::向量::插入...

例外

如果出于任何原因引发异常,则此函数没有效果%28强异常保证%29。%28自C++11%29。

如果手术会导致size() > max_size(),抛std::length_error...

二次

代码语言:javascript
复制
#include <string>
#include <iostream>
 
int main() {
    std::basic_string<char> str = "string";
    const char* cptr = "C-string";
    const char carr[] = "Two and one";
 
    std::string output;
 
    // 1) Append a char 3 times. 
    // Notice, this is the only overload accepting chars.
    output.append(3, '*');
    std::cout << "1) " << output << "\n";
 
    //  2) Append a whole string
    output.append(str);
    std::cout << "2) " << output << "\n";
 
    // 3) Append part of a string (last 3 letters, in this case)
    output.append(str, 3, 3);
    std::cout << "3) " << output << "\n";
 
    // 4) Append part of a C-string
    // Notice, because `append` returns *this, we can chain calls together
    output.append(1, ' ').append(carr, 4);
    std::cout << "4) " << output << "\n";
 
    // 5) Append a whole C-string
    output.append(cptr);
    std::cout << "5) " << output << "\n";
 
    // 6) Append range
    output.append(&carr[3], std::end(carr));
    std::cout << "6) " << output << "\n";
 
    // 7) Append initializer list
    output.append({ ' ', 'l', 'i', 's', 't' });
    std::cout << "7) " << output << "\n";
}

二次

产出:

二次

代码语言:javascript
复制
1) ***
2) ***string
3) ***stringing
4) ***stringing Two 
5) ***stringing Two C-string
6) ***stringing Two C-string and one
7) ***stringing Two C-string and one list

二次

另见

operator+=

appends characters to the end (public member function)

strcat

concatenates two strings (function)

strncat

concatenates a certain amount of characters of two strings (function)

wcscat

appends a copy of one wide string to another (function)

wcsncat

appends a certain amount of wide characters from one wide string to another (function)

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券