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;
}
 

c++ string详解 assign的更多相关文章

  1. Java的String详解

    Java的String详解 博客分类: Java javaStringString详解常用方法  Java的String类在开发时经常都会被使用到,由此可见String的重要性.经过这次认真仔细的学习 ...

  2. Python操作redis字符串(String)详解 (三)

    # -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...

  3. [读书笔记]C#学习笔记八:StringBuilder与String详解及参数传递问题剖析

    前言 上次在公司开会时有同事分享windebug的知识, 拿的是string字符串Concat拼接 然后用while(true){}死循环的Demo来讲解.其中有提及string操作大量字符串效率低下 ...

  4. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  5. Java堆、栈和常量池以及相关String详解

    一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...

  6. String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别

    本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...

  7. String 详解

    String String对象不可变,当对象创建完毕之后,如果内容改变则会创建一个新的String对象,返回到原地址中. 不可变优点: 多线程安全. 节省空间,提高效率. 源码: public fin ...

  8. C++中的string详解

    标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string>  声明一个字符串 声明一个字符串有很多种方式,具体如 ...

  9. Go string 详解

    前言 字符串(string) 作为 go 语言的基本数据类型,在开发中必不可少,我们务必深入学习一下,做到一清二楚. 本文假设读者已经知道切片(slice)的使用,如不了解,可阅读 Go 切片 基本知 ...

随机推荐

  1. @RequestMapping的简单理解

    @Controller public class ItemController { @Autowired private ItemService itemService; 获取路径参数.../item ...

  2. webpack4 单入口文件配置 多入口文件配置 以及常用的配置

    单入口文件配置 webpack.config.js const path = require('path'); const HtmlWebpackPlugin = require('html-webp ...

  3. jQuery的replaceWith()函数用法详解

    replaceWith,替换元素 replaceWith() 方法将选择的元素的内容替换为其他内容. 我们先在先看一个实例 <!DOCTYPE html> <html> < ...

  4. Ranger使用solrCloud存储审计日志

    Ranger使用solrCloud存储审计日志 标签(空格分隔): Ranger 1, Zookeeper 搭建 1,忽略.默认已经搭建好zk 集群. VECS17820:2181,VECS17821 ...

  5. JVM-GC算法(二)-复制算法&&标记整理算法

    这次我和各位分享GC最后两种算法,复制算法以及标记/整理算法.上一篇在讲解标记/清除算法时已经提到过,这两种算法都是在此基础上演化而来的,究竟这两种算法优化了之前标记/清除算法的哪些问题呢? 复制算法 ...

  6. Java多线程核心知识(跳槽面试必备)

    多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...

  7. 当 springboot 部署war包,tomcat报一堆无法解决的问题时

    直接打包 jar即可,这样就可以解决这些问题了.

  8. TCP主动打开 之 第二次握手-接收SYN+ACK

    假设客户端执行主动打开,已经经过第一次握手,即发送SYN包到服务器,状态变为SYN_SENT,服务器收到该包后,回复SYN+ACK包,客户端收到该包,进行主动打开端的第二次握手部分:流程中涉及到的函数 ...

  9. Fastadmin 后台编辑,或者添加的时候,出现的问题

    1.情况如图:编辑的时候,这个关联id,默认查出来的是用户昵称,如果要显示用户名,该怎么修改,不要着急,听我慢慢道来 2.首先要找到 编辑页面,检查问题 3.完成

  10. js实现图片上传到服务器和回显

    关于js实现图片的上传和回显,曾经用户的代码粘在这里: 样式:这样写样式的道理是给<input>标签的父级设置一个背景图,就是‘+’那个背景图,然后把<input>的宽高设置得 ...