当前位置:网站首页>初识string+简单用法(一)
初识string+简单用法(一)
2022-06-24 09:50:00 【i跑跑】
目录
string是什么:
string是类
string类是basic_string模板类的一个实例
使用char来实例化basic_string模板类
实例化出的对象是一个管理动态增长的字符数组,以 ‘\0’ 结尾
使用时需要包含头文件string
#include <string>
c++98中string常用的构造函数
1.构造一个空的类的对象--空字符串
string + (对象名)
string s1;
2.常量字符串初始化
string + 对象名 (“初始化”)
string s2("hello csdn");
3.拷贝构造初始化
string + 对象名 (已存在对象)
string +对象名 = 已存在对象
string s3(s2); string s4 = s3;
上述三个为最常用的初始化方式
4.指定长度初始化。
string + 对象名("初始化内容,初始化长度")
string s5("asdfghjkl", 4);
5.指定字符及其个数初始化
string + 对象名 (个数,字符)
string s6(5,'c');
6.取已存在对象的子串初始化
string + 对象名(已存在对象,指定起始位置,指定长度)
string s7(s2,3,5);
string的赋值
1.对象1=对象2
![]()
2.对象 = 字符串
3. 对象 = 字符
string对象的遍历方式
1.[下标]遍历
void test3() { string s1("abcdefg"); //1.[下标]遍历 //这里用到了size()函数,返回的是字符串长度,不包含\0 cout << s1.size() << endl; for (size_t i = 0;i < s1.size();i++) { //这里用[],本质上是函数重载: s1.operator[](i); cout << s1[i] << " "; } cout << endl; }
string类的对象是自定义类型,用[]时需要找到其运算符重载函数进行调用
2.迭代器正向遍历
void test4() { string s1("abcdefg"); //2.迭代器遍历--iterator属于string中的一个内置类型 //从第一个位置开始 string::iterator it = s1.begin(); //end()是结束位置的下一个位置 while (it!=s1.end()) { cout << *it << " "; it++; } cout << endl; }我们刚开始接触迭代器,可以先将他理解成为指针,begin()指向第一个字符,end()指向最后一个字符的下一个,一般指向'\0',接下来的过程按照指针方式操作即可。
3.迭代器反向遍历
void test7() { string s1("abcdefg"); //3.迭代器反向遍历--reverse_iterator //从第一个位置开始 string::reverse_iterator rit = s1.rbegin(); //end()是结束位置的下一个位置 while (rit != s1.rend()) { cout << *rit << " "; rit++; } cout << endl; }与正向迭代器遍历方向相反,需要注意语法形式,reverse_iterator、rbegin、rend
虽然是反向遍历,但是仍然是++,不是--
4.范围for
void test5() { //3.范围for遍历 string s1("hello csdn"); for (auto e:s1) { cout << e << " "; } cout << endl; }创建一个变量e,根据 s1 用 auto 推出e的类型,遍历时,会自动++,直到遇到 '\0' 结束
范围for的实现中,实际上被替换为了迭代器
string的修改
既然可以遍历访问到每一个字符,那相应的也可以在访问字符时,对其进行修改
1.下标访问修改
2.访问对象中at函数进行修改
string引用传参
string可算作是类型,是可以进行传参的
这里我们直接进行传值传参,形参是实参的一份临时拷贝
当对象较大时,消耗大,因此形参应用引用传参
不改变参数内容是,前面尽量加上const
加上const后是跑不过去的,因为 begin 和 end 返回时存在权限放大的问题
![]()
要解决令权限相同,那么迭代器类型应是const,改动如下:
![]()
string对象的容量开辟
string类型支持查看对象的大小及容量
容量
调用对象的capacity()函数,即可查看容量
但是每次动态开辟都会增大消耗,有什么办法减少开辟空间的次数呢?
reserve
只改变空间的大小,对size没有影响
resize
![]()
string对象的尾插
插入字符push_back
用法如下:
插入字符串append
两种用法:直接插入字符串、插入string类的对象
直接+=
支持字符和字符串,是最广泛的用法
边栏推荐
- How does easydss use go fastdfs distributed file servers to reduce service pressure?
- How to make a good video? What are the operation methods?
- What are the means of network promotion?
- Differences among cookies, session, localstorage and sessionstorage
- Canvas pipe animation JS special effect
- 26.删除有序数组的重复项
- 解决DBeaver SQL Client 连接phoenix查询超时
- Besides technology, programmers also need to master a skill - self marketing ability
- How to use multiple kindeditor editors on a page and pass values to the server
- Image click enlargement and adaptive size in the applet rich text
猜你喜欢

Multithreaded applications - improve efficiency

进程与多线程

Quick completion guide for mechanical arm (II): application of mechanical arm

解决DBeaver SQL Client 连接phoenix查询超时

Plant growth H5 animation JS special effect

Flink cluster construction and enterprise level yarn cluster construction

Appium automation test foundation - mobile end test environment construction (I)

Shape change loader loads jsjs special effect code

机械臂速成小指南(零):指南主要内容及分析方法

JMeter interface test tool foundation - use badboy to record JMeter script
随机推荐
What is a voice assistant? What will the future voice assistant look like?
splice()方法的使用介绍
Multithreaded applications - improve efficiency
机械臂速成小指南(二):机械臂的应用
126. 单词接龙 II BFS
Cookie 、Session、localstorage、Sessionstorage的区别
腾讯开源项目「应龙」成Apache顶级项目:前身长期服务微信支付,能hold住百万亿级数据流处理...
Canvas infinite scan JS special effect code
The difference between the sleep () method and the wait () method of a thread
【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
Act as you like
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
Distributed transaction principle and solution
Image click enlargement and adaptive size in the applet rich text
Canvas falling ball gravity JS special effect animation
Plant growth H5 animation JS special effect
Practice sharing of packet capturing tool Charles
数组怎么转对象,对象怎么转数组
Quick completion guide for mechanical arm (I): development overview of mechanical arm
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
































