c++ string详解 assign
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 );
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 c, n 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
|
|
c++ string详解 assign的更多相关文章
- Java的String详解
Java的String详解 博客分类: Java javaStringString详解常用方法 Java的String类在开发时经常都会被使用到,由此可见String的重要性.经过这次认真仔细的学习 ...
- Python操作redis字符串(String)详解 (三)
# -*- coding: utf-8 -*- import redis #这个redis不能用,请根据自己的需要修改 r =redis.Redis(host=") 1.SET 命令用于设置 ...
- [读书笔记]C#学习笔记八:StringBuilder与String详解及参数传递问题剖析
前言 上次在公司开会时有同事分享windebug的知识, 拿的是string字符串Concat拼接 然后用while(true){}死循环的Demo来讲解.其中有提及string操作大量字符串效率低下 ...
- String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1)
本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...
- Java堆、栈和常量池以及相关String详解
一:在JAVA中,有六个不同的地方可以存储数据: 1. 寄存器(register). 这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据 ...
- String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别
本章主要介绍String和CharSequence的区别,以及它们的API详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string01. ...
- String 详解
String String对象不可变,当对象创建完毕之后,如果内容改变则会创建一个新的String对象,返回到原地址中. 不可变优点: 多线程安全. 节省空间,提高效率. 源码: public fin ...
- C++中的string详解
标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string> 声明一个字符串 声明一个字符串有很多种方式,具体如 ...
- Go string 详解
前言 字符串(string) 作为 go 语言的基本数据类型,在开发中必不可少,我们务必深入学习一下,做到一清二楚. 本文假设读者已经知道切片(slice)的使用,如不了解,可阅读 Go 切片 基本知 ...
随机推荐
- vps能ping通但是ssh无法连接
一.全国ping测试网页https://tools.ipip.net/ping.php 如果100%丢包率,那么肯定被q了,如果没有,也不一定没被q,进入下一步 二.国内外端口扫描测试http://t ...
- jQuery事件之解绑事件
语法: $(selector).unbind([eventType][,handler(eventObject)]); 返回值:jQuery 参数解释: eventTypey:类型:String以后包 ...
- sublime 配置sftp代码自动上传(原)
1.首先安装Package Control 使用 ctrl+`快捷键 或者 菜单项View > Show Console 来调出命令界面 然后复制粘贴下面的Python代码到命令输入框中: im ...
- PHP-windows下IDEA配置网页地址
- RabbitMQ MQTT协议和AMQP协议
RabbitMQ MQTT协议和AMQP协议 1 序言... 1 1.1 RabbitMq结构... 1 1.2 RabbitMq消息接收... 4 1.3 Ex ...
- typedef void(*Fun) (void)是什么意思 函数指针(回调函数) 和函数对象总结
https://blog.csdn.net/FreeApe/article/details/49124043 bool (*pf)(const string &,const string &a ...
- Hadoop-2.7.5完全分布式搭建
1.在虚拟机上安装Hadoop完全分布式准备工作 1)这里使用的是VMWare软件,在VMWare上安装一个CentOS6.5,并再克隆两个机器配置相关MAC地址,以及配置机器名 2)三台虚拟机配置好 ...
- vue绑定样式
使用三目运算符绑定样式 本来以为使用vue模版写法,在绑定单个样式,也就是一个class类名的时候可以直接书写,就像这样 <div id="app"> <div ...
- 20165213 Exp6 信息搜集与漏洞扫描
信息搜集与漏洞扫描 一. 实践内容 (1)各种搜索技巧的应用 利用Google Hacking Datebase搜索. 尝试搜索http相关的漏洞,可以看到漏洞的相关信息. 也可以使用过滤器进行过滤, ...
- 【Java面试宝典】JavaSE_2.1_Java基础● 请你说说Java和PHP的区别?
文章目录 ①eechen的回答 - 知乎 ②h4cd-开源中国 ③乔·沃特金斯-Musings, ninja ones-思考,忍者 什么是准时制? 为什么PHP需要JIT? JIT可以使我的网站更快吗 ...