C++ string 类中的 assign()函数

C++ string 类的成员函数,用于拷贝、赋值操作,它们允许我们顺次地把一个 string 对象的部分内容拷贝到另一个 string 对象上。

函数原型

string &operator=(const string &s);               把字符串s赋给当前字符串
string &assign(const char *s);                 用c类型字符串s赋值
string &assign(const char *s,int n);              用c字符串s开始的n个字符赋值
string &assign(const string &s);                 把字符串s赋给当前字符串
string &assign(int n,char c);                  用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);       把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);  把first和last迭代器之间的部分赋给字符串

函数以下列方式赋值:用str为字符串赋值;用str的开始num个字符为字符串赋值;用str的子串为字符串赋值,子串以index索引开始,长度为len;用num个字符ch为字符串赋值。

成员函数

void assign(input_iteratorstart,input_iteratorend );
void assign( size_type num, constTYPE&val );

assign() 函数要么将区间[start, end)的元素赋到当前vector,或者赋num个值为val的元素到vector中.这个函数将会清除掉为vector赋值以前的内容。官方解释:http://www.cplusplus.com/reference/string/string/assign/原文链接: https://www.cnblogs.com/sylar5/p/6638189.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/251658

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 上午5:26
下一篇 2023年2月14日 上午5:27

相关推荐