当前位置:网站首页>初识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类的对象
直接+=
支持字符和字符串,是最广泛的用法
边栏推荐
- Leetcode-929: unique email address
- What you must know about distributed systems -cap
- Common third-party UI frameworks
- 多线程的应用 - 提升效率
- 分布式事务原理以及解决分布式事务方案
- The latest entry date of SQL Sever test questions
- 栈题目:括号的分数
- 23. opencv - image mosaic project
- Quick completion guide for mechanical arm (II): application of mechanical arm
- Web项目部署
猜你喜欢

26.删除有序数组的重复项

P5.js paper crane animation background JS special effect

Canvas pipe animation JS special effect

Cookie 、Session、localstorage、Sessionstorage的区别

JMeter interface test tool foundation - badboy tool

Solve the timeout of Phoenix query of dbeaver SQL client connection

Leetcode-1823: find the winner of the game

Flink checkPoint和SavePoint

HBuilder制作英雄皮肤抽奖小游戏

88.合并有序数组
随机推荐
[IEEE publication] 2022 International Conference on service robots (iwosr 2022)
【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
Leetcode interview question 16.06: minimum difference
Difference between package type and basic type
SQL Server about like operator (including the problem of field data automatically filling in spaces)
Common third-party UI frameworks
【IEEE出版】2022年自然语言处理与信息检索国际会议(ECNLPIR 2022)
88.合并有序数组
栈题目:函数的独占时间
Rising bubble canvas breaking animation JS special effect
What are the means of network promotion?
What is a compressed file? What are the advantages of different methods of compressing files?
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
抓包工具charles实践分享
Shape change loader loads jsjs special effect code
Web project deployment
A method of generating non repeated numbers in nodejs
Wechat applet rich text picture width height adaptive method introduction (rich text)
【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
[IEEE publication] 2022 International Conference on intelligent transportation and future travel (cstfm 2022)
































