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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// string::assign
#include <iostream>
#include <string>
using namespace std; 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" return 0;
}
 

最新文章

  1. markdown 常用语法 (在macdown内使用正常)
  2. job1
  3. 基于Redis、Storm的实时数据查询实践
  4. PAT/进制转换习题集
  5. Windows 10 Weather App无法正常显示解决方法
  6. java switch语句注意的事项
  7. Sprint第三个冲刺(第一天)
  8. 广州传智博客黑马训练营.Net15期
  9. Android 内存优化 (防Memory Leak)
  10. SQL Server 小技巧【2】
  11. 高扩展的基于NIO的服务器架构
  12. 搜索引擎的提示效果完整的JavaScript代码
  13. iis6配置使用页面Gzip压缩提速
  14. iOS 10权限崩溃问题
  15. LOJ #6043. 「雅礼集训 2017 Day7」蛐蛐国的修墙方案
  16. MYSQL数据库高可用方案探究
  17. placeholder兼容性问题
  18. python 最简单的爬虫
  19. 怎样借助Python爬虫给宝宝起个好名字--python 学习
  20. 转载 hibernate一级缓存和二级缓存的区别

热门文章

  1. linux下安装虚拟环境
  2. 使用Navicat客户端运行SQL语句出现中文乱码
  3. Trying to get property &#39;art_id&#39; of non-object
  4. 【Java Web】简易商品信息管理系统——首个Web项目
  5. 关于在mac上使用valet集成环境添加memcache扩展
  6. linux安装vlc视频播放器
  7. 显示和隐藏(display属性)
  8. DP----鬼畜的数字三角形
  9. windos批处理启动redis与哨兵
  10. 凸包Graham Scan算法实现