string 的介绍及使用

一.string类介绍

        C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。

        C++中将string封装为单独的类,string 类是 C++ 标准库中的一个非常重要的类,用于表示和操作字符串。string类位于命名空间std(标准库)下,使用string类记得加上头文件#include,并且使用命名空间using namespace std或者using std::string。注意:string低层还是模版。

二.string类的常用接口

1.构造函数(constructor)

1、无参构造:string(); 构造空的string类对象,即空字符串。常用。

2、有参构造:string (const char* s); 用常量字符串来构造string类对象常用。

3、拷贝构造:string (const string& str); 用str拷贝构造string类对象常用。
4、string (const string& str, size_t pos, size_t len = npos); 构造从下标pos开始,长度为len的子串,含缺省参数npos。
5、string (const char* s, size_t n); 构造前n个字符组成的子串。
6、string (size_t n, char c); 构造n个字符c组成的字符串。

int main()
{string s1;string s2("hello xzy");string s3(s2);string s4(s2, 6, 3);string s5("hello xzy", 5);string s6(10, 'x');cout << s1 << endl;//输出:cout << s2 << endl;//输出:hello xzycout << s3 << endl;//输出:hello xzycout << s4 << endl;//输出:xzycout << s5 << endl;//输出:hellocout << s6 << endl;//输出:xxxxxxxxxxreturn 0;
}

2、析构函数(destructor)

~string(); 程序结束前自动调用,释放堆区动态开辟的资源

3.运算符重载(operator )

1.operator=

  1. string& operator= (const string& str); 常用。
  2. string& operator= (const char* s);
  3. string& operator= (char c);
int main()
{string s1;string s2;string s3;//赋值重载s1 = "hello xzy";s2 = s1;s3 = 'v';//拷贝构造string s4 = s1;cout << s1 << endl;//输出:hello xzycout << s2 << endl;//输出:hello xzycout << s3 << endl;//输出:vcout << s4 << endl;//输出:hello xzyreturn 0;
}

2.operator[ ]

int main()
{string s1("hello xzy");s1[6] = 'w';s1[7] = 'j';s1[8] = '\0';cout << s1 << endl; //输出:hello wjs1[10] = 'A'; //下标越界,内部断言assert报错return 0;
}

3.operator+=

  1. string& operator+= (const string& str); 常用。
  2. string& operator+= (const char* s);
  3. string& operator+= (char c);
int main()
{string s1("hello xzy");string s2(" how are you");s1 += s2;cout << s1 << endl;s1 += "???";cout << s1 << endl;s1 += '!';cout << s1 << endl;return 0;
}

4.operator+

  1. string operator+ (const string& lhs, const string& rhs);
  2. string operator+ (const string& lhs, const char* rhs);
  3. string operator+ (const char* lhs, const string& rhs);
int main()
{string s1("hello");string s2 = s1 + " world";string s3 = "xzy " + s1;string s4 = s2 + s3;cout << s2 << endl; //hello worldcout << s3 << endl; //xzy hellocout << s4 << endl; //hello worldxzy helloreturn 0;
}

 4、string的四种迭代器(iterator)

        迭代器是一种用于遍历容器元素的对象(并非类,而是设计模式中的一种行为模式),它提供了一种通用的访问容器元素的方式,无论容器的类型和数据结构如何。迭代器在C++标准库中被广泛使用,特别是在处理如vector、list、map等容器时。

1.正向迭代器 iterator

返回正向迭代器:可以修改字符串。

1、iterator begin(); 返回字符串的第一个字符。

2、iterator end(); 返回字符串最后一个有效字符(不含\0)的下一个字符。

int main()
{string s1("hello ryc");string::iterator it = s1.begin();while (it != s1.end()){cout << *it << " ";++it;}cout << endl;return 0;
}

 2.反向迭代器 reverse_iterator

返回反向迭代器:可以修改字符串。

reverse_iterator rbegin(); 返回字符串最后一个有效字符(不含\0)。

reverse_iterator rend(); 返回字符串第一个字符的前一个字符。


int main()
{string s1("hello ryc");string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){cout << *rit << " ";rit++;}cout << endl;return 0;
}

3.const修饰的正向迭代器 const_iterator

返回const修饰的正向迭代器:不可以修改字符串。

  1. const_iterator begin() const;
  2. const_iterator end() const;

 4.const修饰的反向迭代器 const_reverse_iterator

返回const修饰的反向迭代器:不可以修改字符串。

  1. const_reverse_iterator rbegin() const;
  2. const_reverse_iterator rend() const;

 5.string类对象的容量操作

  1. size_t size() const; 返回字符串有效字符长度(不包括\0)。常用。
  2. size_t length() const; 返回字符串有效字符长度(不包括\0)。
  3. size_t capacity() const; 返回空间总大小(不包括\0)。常用。
  4. void resize (size_t n); 为字符串预留大于等于n的空间(不包括\0),避免扩容,提高效率。常用。
  5. void clear(); 清空数据,但是一般不清容量。常用。
  6. bool empty() const; 判断是否为空。常用。

注意:

  1. size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
  2. clear()只是将string中有效字符清空,不改变底层空间大小。

6.string类对象的修改操作

  1. void push_back (char c); 在字符串后尾插字符c。
  2. void pop_back(); 在字符串尾删一个字符。
  3. string& append (const string& str); 在字符串后追加一个字符串。
  4. string& assign (const string& str, size_t subpos, size_t sublen); 拷贝字符串:从下标为subpos开始,拷贝长度为sublen的字符串到string类对象里面。
  5. string& insert (size_t pos, const string& str); 在pos位置处插入字符串到string类对象里面。(由于效率问题(移动数据),谨慎使用)。
  6. string& erase (size_t pos = 0, size_t len = npos); 从pos位置开始删除长度为npos个字符。(由于效率问题(移动数据),谨慎使用)。
  7. void swap (string& str); 交换字符串。
  8. string& replace (size_t pos, size_t len, const string& str); 从pos位置开始的长度为len的子串,替换为str。(伴随着插入与删除,效率低,谨慎使用)。
int main()
{string s1("hello ryc");s1.push_back('!');cout << s1 << endl;s1.pop_back();cout << s1 << endl;s1.append("666");cout << s1 << endl;//可以使用+=代替尾差s1 += "maldsk";cout << s1 << endl;s1.insert(0, "why");cout << s1 << endl;string s2("why did you do so? I don't know!");s2.erase(0, 1);cout << s2 << endl;s2.replace(0,2,"adw2y");cout << s2 << endl;string s5("hello x hello x");string tmp;tmp.reserve(s5.size());for (auto ch : s5){if (ch == 'x'){tmp += "xy";}else{tmp += ch;}}cout << tmp << endl;swap(tmp, s5);cout << s5 << endl;return 0;
}

9、const char* c_str() const; 返回C格式字符串。方便调用C中的接口。

7.string类对象的查找操作

  1. string substr (size_t pos = 0, size_t len = npos) const; 找子串:返回从pos位置开始,长度为npos的string类。
  2. size_t find (char c, size_t pos = 0) const; 从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置。
  3. size_t rfind (char c, size_t pos = npos) const; 从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置。找不到返回-1。
  4. size_t find_first_of (const char* s, size_t pos = 0) const; 从字符串pos位置开始从前往后找字符串s中出现的字符,返回该字符在字符串中的位置。
  5. size_t find_last_of (const char* s, size_t pos = npos) const; 从字符串pos位置开始从后往前找字符串s中出现的字符,返回该字符在字符串中的位置。
  6. size_t find_first_not_of (const char* s, size_t pos = 0) const; 从字符串pos位置开始从前往后找字符串s中没有出现的字符,返回该字符在字符串中的位置。
  7. size_t find_last_not_of (const char* s, size_t pos = npos) const; 从字符串pos位置开始从后往前找字符串s中没有出现的字符,返回该字符在字符串中的位置。
int main()
{//suffix:后缀string s1("test.cpp");size_t pos1 = s1.find(".");string suffix1 = s1.substr(pos1);cout << suffix1 << endl; //.cppstring s2("test.cpp.zip");size_t pos2 = s2.rfind(".");string suffix2 = s2.substr(pos2);cout << suffix2 << endl; //.zipstring s3("hello ryc");size_t found = s3.find_first_of("ryc");while (found != string::npos){s3[found] = '*';found = s3.find_first_of("ryc", found + 1);}cout << s3 << endl; //hello ***string str1("/user/bin/man");cout << endl << str1 << "的路径名与文件名如下:" << endl;size_t found1 = str1.find_last_of("/\\");cout << "path:" << str1.substr(0, found1) << endl;cout << "file:" << str1.substr(found1 + 1) << endl;string str2("c:\\windows\\winhelp.exe");cout << endl << str2 << "的路径名与文件名如下:" << endl;size_t found2 = str2.find_last_of("/\\");cout << "path:" << str2.substr(0, found2) << endl;cout << "file:" << str2.substr(found2 + 1) << endl;return 0;
}

8.string类对象的遍历操作

1.下标 + []


int main()
{string s1("hello ryc");for (int i = 0; i < s1.size(); i++){s1[i] += 2;cout << s1[i] << " ";}cout << endl << s1 << endl;return 0;
}

2.迭代器

int main()
{string s1("hello ryc");string::iterator it = s1.begin();while (it != s1.end()){*it += 2;//可以修改cout << *it << " ";++it;}cout << endl << s1 << endl;return 0;
}

3.auto和范围for

int main()
{string s1("hello ryc");//范围for 自动迭代 自动判断结束//底层就是迭代器for (auto ch : s1){ch += 2;//修改ch对s1无影响,ch是它的拷贝cout << ch << " ";}cout << endl << s1 << endl;return 0;
}
int main()
{string s1("hello ryc");//范围for 自动迭代 自动判断结束//底层就是迭代器for (auto& ch : s1){ch += 2;//修改ch对s1无影响,ch是它的拷贝//加上引用即可!cout << ch << " ";}cout << endl << s1 << endl;return 0;
}

1.auto关键字

  1. 在早期C/C++中auto的含义是:使用auto修饰的变量,是具有自动存储器的局部变量,后来这个不重要了。C++11中,标准委员会变废为宝赋予了auto全新的含义即:auto不再是一个存储类型指示符,而是作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期 推导而得。
  2. 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&。
  3. 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际只对第一个类型进行推导,然后用推导出来的类型定义其他变量。
  4. auto不能作为函数的参数,可以做返回值,但是建议谨慎使用。
  5. auto不能直接用来声明数组。

2.范围for

  1. 对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此C++11中引入了基于范围的for循环。for循环后的括号由冒号“ :”分为两部分:第一部分是范围内用于迭代的变量,第二部分则表示被迭代的范围,自动迭代,自动取数据,自动判断结束。
  2. 范围for可以作用到数组和容器对象上进行遍历。
  3. 范围for的底层很简单,容器遍历实际就是替换为迭代器,这个从汇编层也可以看到。
int main()
{int array[] = { 1,2,3,4,5 };for (auto i : array){cout << i << " ";}cout << endl;return 0;
}

三、非成员函数:getline()

istream& getline (istream& is, string& str, char delim); delim:分隔符
istream& getline (istream& is, string& str);

类似C语言中的scanf(“%s”, str),但是其遇到空格会停止;
C++中引入了getline优化了scanf遇到的问题,默认遇到\n才停止,也可以自定义停止字符delim。

#include <iostream>
#include<string>
using namespace std;int main() 
{string str;getline(cin, str);size_t pos = str.rfind(' ');string sub = str.substr(pos + 1);cout << sub.size() << endl;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.xdnf.cn/news/1544860.html

如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!

相关文章

服务设计原则介绍

在Java或任何软件开发中&#xff0c;设计服务时遵循一些核心原则是非常重要的&#xff0c;这些原则不仅有助于构建高质量、可维护的软件系统&#xff0c;还能提高系统的可扩展性和可重用性。以下是一些关键的服务设计原则&#xff1a; 单一职责原则&#xff08;SingleResponsib…

个人量化成功之路-----获取实时OHLC的数据

昨天有一个客户说自己之前交易主要看OHLC线&#xff0c;想问量化软件如何实现获取实时一分钟OHLC的数据并生产图像。 有朋友可能不熟悉OHLC这个名字哈&#xff0c;其实跟K线/蜡烛图的数据是一样的&#xff0c;和蜡烛图的区别只是表现形式的不一致。 O为open、开盘价&#xff…

第6章 常用UI组件库

一.Element Plus组件库 1. 安装Element Plus 什么是Element Plus&#xff1f; Element Plus是基于Vue 3开发的优秀的PC端开源UI组件库&#xff0c;它是Element的升级版&#xff0c;对于习惯使用Element的人员来说&#xff0c;在学习Element Plus时&#xff0c;不用花费太多的…

如何使用ssm实现基于Java的超市管理系统

TOC ssm681基于Java的超市管理系统jsp 绪论 1.1 研究背景 当前社会各行业领域竞争压力非常大&#xff0c;随着当前时代的信息化&#xff0c;科学化发展&#xff0c;让社会各行业领域都争相使用新的信息技术&#xff0c;对行业内的各种相关数据进行科学化&#xff0c;规范化…

BUUCTF [SCTF2019]电单车详解两种方法(python实现绝对原创)

使用audacity打开&#xff0c;发现是一段PT2242 信号 PT2242信号 有长有短&#xff0c;短的为0&#xff0c;长的为1化出来 这应该是截获电动车钥匙发射出的锁车信号 0 01110100101010100110 0010 0前四位为同步码0 。。。中间这20位为01110100101010100110为地址码0010为功…

macOS设置 Redis自启动

macOS自定义开机启动程序 1、打开 自动操作app里面的应用程序 过程资料 1、https://juejin.cn/post/7123098435254747149 2、https://blog.twofei.com/889/ 2、编写脚本&#xff0c;可以点击右上角运行测试&#xff0c;保存为 app https://juejin.cn/post/7123098435254747149…

全能的Office插件——不坑盒子 2024.0923发布,云同步配置、合并单元格复制、PPT样机展示……

昨天凌晨&#xff0c;不坑盒子上线了2024.0923版本&#xff0c;这次更新的功能比较多&#xff0c;亮点较多&#xff0c;有必要发文推荐给大家&#xff01; 向新人介绍 不坑盒子是一款全能的Office插件&#xff0c;支持微软Office和WPS Office的办公三件套&#xff08;Word、E…

众数信科 AI智能体政务服务解决方案——AI法律助手

政务服务解决方案 AI法律助手 一款基于AI大模型的智能鼠标 搭裁“寻知AI法律助手” 众数信科AI智能体 产品亮点 能够结合全国各地案例数据 为用户提供法律咨询、文书生成、案例研判 类案推荐、法规检索等法律服务 同时结合Al office插件 具备AI智能办公、PPT生成等功能 …

《动手学深度学习》笔记1.7——模型选择 + 过拟合-欠拟合

目录 1. 模型选择 1.1 训练误差 vs. 泛化误差 1.2 验证数据集 vs. 测试数据集 1.3 K-折交叉验证 1.4 总结 2. 过拟合与欠拟合&#xff08;核心知识&#xff09; 2.1 过拟合 vs. 欠拟合 2.2 模型容量 2.3 估计模型容量 2.4 VC维 衡量模型容量 2.5 数据复杂度 3. 代…

nginx如何拦截未经授权的跳转

nginx如何拦截未经授权的跳转 背景准备好一个网站准备好引用网站配置nginx拦截效果 背景 在现实工作中往往有一些企业或人未取得授权但是转载或挂载我们的网址。那么有些要求严格或者有其他原因的情况下不希望这些链接正常访问。所以就有了这样的需求。前提是咱们的网站什么的是…

linux网络编程7

24.9.24学习目录 一.网络通信过程&#xff08;续&#xff09;1.路由器 二.原始套接字&#xff08;SOCK_RAW&#xff09;1.创建原始套接字2.数据包解析 一.网络通信过程&#xff08;续&#xff09; 1.路由器 路由器是用于连接多个逻辑上分开的网络&#xff1b; 具有判断网络地…

谷歌收录查询工具,有哪些常见的可以查询谷歌收录情况的工具

在SEO&#xff08;搜索引擎优化&#xff09;领域中&#xff0c;了解网站在谷歌搜索引擎中的收录情况是至关重要的。这有助于网站所有者评估网站的可见性和优化效果。以下是一些常见的、可用于查询谷歌收录情况的工具&#xff0c;它们各自具有独特的功能和优势。 1.GoogleSe…

Vue2是什么?有什么用?超详细+通俗易懂版!

Vue2是一种流行的JavaScript前端框架&#xff0c;由尤雨溪&#xff08;Evan You&#xff09;开发&#xff0c;并于2014年首次发布。它旨在使用户能够更轻松地构建用户界面&#xff0c;具有一系列显著的特点和优势&#xff0c;使其成为前端开发领域的重要工具。 Vue2的主要特点包…

VMWare虚拟机键盘卡顿

文章目录 环境问题解决办法参考 环境 Windows 11 家庭中文版VMware Workstation 17 ProUbuntu 24.04.1 问题 最近新入手了一台电脑台式机&#xff0c;型号是联想拯救者刃7000K&#xff0c;自带Win11家庭版。主机的CPU是第14代英特尔酷睿i9处理器&#xff0c;异构24核32线程。…

​无文字高德电子地图分享

如果你有对高德的电子地图进行过自定义加载&#xff0c;你应该知道高德的电子地图是有带文字标注的&#xff0c;这里为你分享无文字版高德电子地图。 普通电子地图 打开下面的网址进入水经微图&#xff08;简称“微图”&#xff09;网页版。 https://map.wemapgis.com 点击…

力扣题解2207

大家好&#xff0c;欢迎来到无限大的频道。 今日继续给大家带来力扣题解。 题目描述&#xff08;中等&#xff09;​&#xff1a; 字符串中最多数目的子序列 给你一个下标从 0 开始的字符串 text 和另一个下标从 0 开始且长度为 2 的字符串 pattern &#xff0c;两者都只包…

DataWhale X 南瓜书学习笔记 task03笔记

对数几率回归 使用场景&#xff1a;分类任务。根据广义线性模型&#xff0c;分类任务构建模型的基本思想&#xff1a;找到一个单调可微函数将分类任务的真实标记&#xff08;值&#xff09;与线性回归模型的预测值联系起来。 对数几率回归的引入 二分类任务 输出标记&#…

windows下成功运行MicroRTS-Py项目

1.microRTS&#xff08;java&#xff09; microRTS是java写的跨平台的小型即时战略模拟器。 Farama-Foundation/MicroRTS: A simple and highly efficient RTS-game-inspired environment for reinforcement learning (github.com)https://github.com/Farama-Foundation/Micr…

828华为云征文|华为云Flexus X实例:极速搭建个人代码仓库GitLab平台

目录 前言 一、Flexus云服务器X介绍 1.1 Flexus云服务器X实例简介 1.2 Flexus云服务器X实例特点 1.3 Flexus云服务器X实例使用场景 二、Flexus云服务器X购买 2.1 Flexus X实例购买 2.2 重置密码 2.3 登录服务器 三、Flexus X 实例安装GitLab 3.1 GitLab镜像下载 3.2 GitLab部署…

Arthas mbean(查看 Mbean 的信息)

文章目录 二、命令列表2.1 jvm相关命令2.1.10 mbean&#xff08;查看 Mbean 的信息&#xff09;举例1&#xff1a;列出所有 Mbean 的名称&#xff1a;举例2&#xff1a;查看 Mbean 的元信息&#xff1a;举例3&#xff1a;查看 mbean 属性信息&#xff1a;举例4&#xff1a;mbea…