标准库中的string类(上)——“C++”

各位CSDN的uu们好呀,好久没有更新小雅兰的C++专栏的知识啦,接下来一段时间,小雅兰就又会开始更新C++这方面的知识点啦,以及期末复习的一些知识点,下面,让我们进入西嘎嘎string的世界吧!!!


首先,在学习西嘎嘎的过程中,我们需要学会去看文档!!!

西嘎嘎官方文档:cppreference.com

由于这个文档比较乱,所以一般用这个:cplusplus.com - The C++ Resources Network


string类的常用接口说明


string类的常用接口说明

string类对象的常见构造

https://cplusplus.com/reference/string/string/string/ 

函数名称功能说明
string() (重点)构造空的string类对象,即空字符串
string(const char* s) (重点) 用C-string来构造string类对象
string(const string&s) (重点) 拷贝构造函数
string(size_t n, char c) string类对象中包含n个字符c
int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
}

 如果是想要把这些内容打印出来的话,就直接cout就可以了,因为库函数中已经重载了opeator<<!!!

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	return 0;
}

这些就是最常见的,其余的只需要了解一下就可以了,需要的时候直接查文档! 

 string (const string& str, size_t pos, size_t len = npos);

substring constructor
Copies the portion of str that begins at the character position pos and spans len characters (or until the end of str, if either str is too short or if len is string::npos). 

str:Another string object, whose value is either copied or acquired.

pos:Position of the first character in str that is copied to the object as a substring.
If this is greater than str's length, it throws out_of_range.
Note: The first character in str is denoted by a value of 0 (not 1).

len:Length of the substring to be copied (if the string is shorter, as many characters as possible are copied).
A value of string::npos indicates all characters until the end of str.

n:Number of characters to copy.

int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	string s5(s2, 0, 5);
	cout << s5 << endl;
	return 0;
}

 

 

static const size_t npos = -1//整型的最大值
int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	string s5(s2, 0, 5);
	cout << s5 << endl;
	//下面这两种写法都是从字符串1的位置,取到结束
	string s6(s2, 1);
	string s7(s2, 1, 100);
	cout << s6 << endl;
	cout << s7 << endl;
	return 0;
}

 

string (const char* s, size_t n);

from buffer

Copies the first n characters from the array of characters pointed by s.

意思是从这个第n个字符开始拷贝,拷贝到目标字符串中!

int main()
{
    string s1;//构造空的string类对象s1
    string s2("hello world");//用C格式字符串构造string类对象s2
    string s3 = s2;//拷贝构造s3
    string s4(s2);//拷贝构造s4
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    string s5(s2, 0, 5);
    cout << s5 << endl;
    //下面这两种写法都是从字符串1的位置,取到结束
    string s6(s2, 1);
    string s7(s2, 1, 100);
    cout << s6 << endl;
    cout << s7 << endl;
    string s8(s2, 5);
    cout << s8 << endl;
    return 0;

 可是,最后的打印结果为什么会是这样的呢?怎么会是打印 world呢?难道不应该是打印hello吗?

原来是用错了!它自动匹配到了上一个函数:string (const string& str, size_t pos, size_t len = npos); 

所以:string s8(s2, 5); 这句代码的意思是:从s2的第五个字符开始拷贝,一直拷贝到最后,直到后面再也没有内容!

应该这么写:

int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	string s5(s2, 0, 5);
	cout << s5 << endl;
	//下面这两种写法都是从字符串1的位置,取到结束
	string s6(s2, 1);
	string s7(s2, 1, 100);
	cout << s6 << endl;
	cout << s7 << endl;
	string s8(s2, 5);
	cout << s8 << endl;
	string s9("hello world", 5);
	cout << s9 << endl;
	return 0;
}


 

string (size_t n, char c);

 fill constructor

Fills the string with n consecutive(连续的) copies of character c.

int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	string s5(s2, 0, 5);
	cout << s5 << endl;
	//下面这两种写法都是从字符串1的位置,取到结束
	string s6(s2, 1);
	string s7(s2, 1, 100);
	cout << s6 << endl;
	cout << s7 << endl;
	string s8(s2, 5);
	cout << s8 << endl;
	string s9("hello world", 5);
	cout << s9 << endl;
	string s10(10, 'l');
	cout << s10 << endl;
	return 0;
}

 

 string的析构函数:

赋值运算符重载:

int main()
{
	string s1;//构造空的string类对象s1
	string s2("hello world");//用C格式字符串构造string类对象s2
	string s3 = s2;//拷贝构造s3
	string s4(s2);//拷贝构造s4
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	string s5(s2, 0, 5);
	cout << s5 << endl;
	//下面这两种写法都是从字符串1的位置,取到结束
	string s6(s2, 1);
	string s7(s2, 1, 100);
	cout << s6 << endl;
	cout << s7 << endl;
	string s8(s2, 5);
	cout << s8 << endl;
	string s9("hello world", 5);
	cout << s9 << endl;
	string s10(10, 'l');
	cout << s10 << endl;
	cout<<endl;
	s1 = s2;
	cout << s1 << endl;
	s1 = "world";
	cout << s1 << endl;
	s1 = 'l';
	cout << s1 << endl;
	return 0;
}

 

这个赋值支持得很宽泛!因为写了很多重载版本!但是平时用的比较多的是第一种!!!


string类对象的访问及遍历操作

函数名称功能说明
operator[] (重 点)返回pos位置的字符,const string类对象调用
begin+endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭 代器
rbegin+endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭 代器
范围forC++11支持更简洁的范围for的新遍历方式

 

 

int main()
{
	string s1("hello world");
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
}

int main()
{
    string s1("hello world");
    cout << s1.size() << endl;
    cout << s1.length() << endl;
    for (size_t i = 0; i < s1.size(); i++)
    {
        //cout << s1[i] << " ";
        cout << s1.operator[](i);————底层原理
    }
    cout << endl;

 

[]既可以读,又可以写!!!

int main()
{
	string s1("hello world");
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
		//cout << s1.operator[](i);
	}
	cout << endl;
	s1[0] = 'z';
	cout << s1 << endl;
}

 

 那如果我现在想要把string逆置一下,那就简单了!

int main()
{
	string s1("hello world");
	cout << s1.size() << endl;
	cout << s1.length() << endl;
	for (size_t i = 0; i < s1.size(); i++)
	{
		cout << s1[i] << " ";
		//cout << s1.operator[](i);
	}
	cout << endl;
	s1[0] = 'z';
	cout << s1 << endl;

	//把string逆置一下
	int begin = 0;
	int end = s1.size() - 1;
	while (begin < end)
	{
		char tmp = s1[begin];
		s1[begin] = s1[end];
		s1[end] = tmp;
		begin++;
		end--;
	}
	cout << s1 << endl;
	return 0;
}

 这只是其中一种写法,可是,在西嘎嘎中,写这个交换,其实是没有意义的,因为:库函数中给我们提供了这个接口!!!

 

int main()
{
    string s1("hello world");
    cout << s1.size() << endl;
    cout << s1.length() << endl;
    for (size_t i = 0; i < s1.size(); i++)
    {
        cout << s1[i] << " ";
        //cout << s1.operator[](i);
    }
    cout << endl;
    s1[0] = 'z';
    cout << s1 << endl;

    //把string逆置一下
    int begin = 0;
    int end = s1.size() - 1;
    while (begin < end)
    {
        /*char tmp = s1[begin];
        s1[begin] = s1[end];
        s1[end] = tmp;*/
        swap(s1[begin], s1[end]);
        begin++;
        end--;
    }
    cout << s1 << endl;
    return 0;
}

 

 

 第二种遍历方式:iterator(迭代器)

string::iterator it = s1.begin();
while (it != s1.end())
{
	cout << *it << " ";
	++it;
}
cout << endl;

也可以修改!

string::iterator it = s1.begin();
while (it != s1.end())
{
	*it += 1;
	cout << *it << " ";
	++it;
}
cout << endl;

 

iterator的用法有点像指针!!!但是只是像指针,而不是就是指针!

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator vit = v.begin();
while (vit != v.end())
{
	cout << *vit << " ";
	++vit;
}
cout << endl;
list<double> lt;
lt.push_back(1.1);
lt.push_back(2.1);
lt.push_back(3.1);
lt.push_back(4.1);
list<double>::iterator lit = lt.begin();
while (lit != lt.end())
{
	cout << *lit << " ";
	++lit;
}
cout << endl;

 


之前,我们手撕了一个string的逆置,但其实,西嘎嘎的库函数里面就有这个函数!

 

string::iterator it = s1.begin();
while (it != s1.end())
{
    *it += 1;
    cout << *it << " ";
    ++it;
}
cout << endl;
reverse(s1.begin(), s1.end());
cout << s1 << endl;

vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator vit = v.begin();
while (vit != v.end())
{
    cout << *vit << " ";
    ++vit;
}
cout << endl;
reverse(v.begin(), v.end());
vit = v.begin();
while (vit != v.end())
{
    cout << *vit << " ";
    ++vit;
}
cout << endl;


list<double> lt;
lt.push_back(1.1);
lt.push_back(2.1);
lt.push_back(3.1);
lt.push_back(4.1);
list<double>::iterator lit = lt.begin();
while (lit != lt.end())
{
    cout << *lit << " ";
    ++lit;
}
cout << endl;
reverse(lt.begin(), lt.end());
lit = lt.begin();
while (lit != lt.end())
{
    cout << *lit << " ";
    ++lit;
}
cout << endl;

 

 string、vector、list都逆置过来了!!!

 之前的那个operator[],提供了两个版本:

     char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;

 

第一个可以编译通过,第二个不能编译通过!

 编译器会去找更匹配的!

 

 

int main()
{
	string s1("hello world");
	const string s2("hello world");
	s1[0] = 'x';
	//s2[0] = 'x';
	cout << s2[0] << endl;

	string::const_iterator it = s2.begin();
	while (it != s2.end())
	{
		//*it += 1;
		cout << *it << " ";
		++it;
	}
	cout << endl;
	return 0;
}

 

 for (auto e : s1)
{
    cout << e << " ";
}
cout << endl;

但是实际上,迭代器才是yyds! 


所以,现在我们遍历,就有三种方法:

  • 下标+[]
  • 迭代器
  • 范围for

但是,主流还是迭代器!!!

 

 

void func(const string& s)
{
	string::const_reverse_iterator it = s.rbegin();
	while (it != s.rend())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}
int main()
{
	string s1("hello world");
	string::reverse_iterator it1 = s1.rbegin();
	while (it1 != s1.rend())
	{
		cout << *it1 << " ";
		++it1;
	}
	cout << endl;
	func(s1);
	return 0;
}


 


好啦,小雅兰今天的西嘎嘎string的使用部分就到这里啦,下一篇博客继续string的使用,后续还会写string模拟实现,加油!!!

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

智能优化算法应用:基于混沌博弈算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于混沌博弈算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于混沌博弈算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.混沌博弈算法4.实验参数设定5.算法结果6.…

HamronyOS 自动化测试框架使用指南

概述 为支撑 HarmonyOS 操作系统的自动化测试活动开展&#xff0c;我们提供了支持 JS/TS 语言的单元及 UI 测试框架&#xff0c;支持开发者针对应用接口进行单元测试&#xff0c;并且可基于 UI 操作进行 UI 自动化脚本的编写。 本指南重点介绍自动化测试框架的主要功能&#x…

跟着我学Python进阶篇:01.试用Python完成一些简单问题

往期文章 跟着我学Python基础篇&#xff1a;01.初露端倪 跟着我学Python基础篇&#xff1a;02.数字与字符串编程 跟着我学Python基础篇&#xff1a;03.选择结构 跟着我学Python基础篇&#xff1a;04.循环 跟着我学Python基础篇&#xff1a;05.函数 跟着我学Python基础篇&#…

c语言力扣题目:消失的数字(有关时间复杂度O(N²)O(N))以及对异或操作符的更深入的理解(如何用人脑的十进制去考量二进制)

目录 Way One :暴力求解,时间复杂度为 O(N) 代码1 Way Two : 时间复杂度限制到 O(N) 代码及其详解 如题 Way One :暴力求解,时间复杂度为 O(N) 大体思路:比如这里我们需要处理的整型数组是"3,0,1",我们可以用冒泡排序或者 qsort函数将他从大到小进行排序成"…

纳米流体传热CFD模拟仿真

纳米流体传热CFD模拟仿真 一、引言 纳米流体传热是当前研究热点之一,由于其独特的传热特性和应用前景,受到了广泛关注。计算流体动力学(CFD)模拟作为一种有效的研究手段,在纳米流体传热领域发挥着重要作用。本文将介绍纳米流体传热CFD模拟的基本原理、方法、应用及未来发…

100GPTS计划-AI编码CodeWizard

地址 https://chat.openai.com/g/g-vX7yfHNcC-code-wizard https://poe.com/CodeWizardGPT 测试 sql 优化 select a.id,a.name,count(b.id),count(c.id) from product a LEFT JOIN secretkey b on a.id b.productId group by a.id LEFT JOIN secretkey c on a.id c.pr…

SLAM算法与工程实践——RTKLIB编译

SLAM算法与工程实践系列文章 下面是SLAM算法与工程实践系列文章的总链接&#xff0c;本人发表这个系列的文章链接均收录于此 SLAM算法与工程实践系列文章链接 下面是专栏地址&#xff1a; SLAM算法与工程实践系列专栏 文章目录 SLAM算法与工程实践系列文章SLAM算法与工程实践…

node.js mongoose middleware

目录 官方文档 简介 定义模型 注册中间件 创建doc实例&#xff0c;并进行增删改查 方法名和注册的中间件名相匹配 执行结果 分析 错误处理中间件 手动抛出错误 注意点 官方文档 Mongoose v8.0.3: Middleware 简介 在mongoose中&#xff0c;中间件是一种允许在执…

Linux静态ip

Linux静态ip Ⅰ、修改静态ip Ⅰ、修改静态ip 修改静态ip必须是root用户 su root //切换root用户 ip a //查看修改前的动态ipvi /etc/sysconfig/network-scripts/ifcfg-ens33 //打开网卡配置文件&#xff0c;修改一处&#xff0c;新增四处 BOOTPROTO&quo…

NumPy教程(一)—— ndarray:多维数组对象

前言 该numpy学习笔记参考了菜鸟教程网、b站up主 孙兴华zz 的《孙兴华中文讲python数据分析三部曲》以及《北理-python数据分析与展示》&#xff0c;课本推荐使用《利用python进行数据分析》 Numpy简介&#xff1a; NumPy(Numerical Python) 是 Python 语言的一个扩展程序库&a…

RHCE8 资料整理(十一)

RHCE8 资料整理 第 32 章 控制语句32.1 判断语句 when32.1.1 when 判断中>、<、!和的使用32.1.2 when 判断中 in的用法32.1.3 when 判断中 is的用法 32.2 判断语句 block-rescue32.3 循环语句 第 32 章 控制语句 一个play中可以包含多个task&#xff0c;如果不想所有的t…

电子学会C/C++编程等级考试2022年06月(六级)真题解析

C/C++等级考试(1~8级)全部真题・点这里 第1题:小白鼠再排队2 N只小白鼠(1 < N < 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从小到大的顺序输出它们头上帽子的颜色。帽子的颜色用 “red”,“blue”等字符串来表示。不同的小白…

董宇辉“回归”成为东方甄选高级合伙人,尘埃落地后是谁赢了?

董宇辉“回归”成为东方甄选高级合伙人&#xff0c;尘埃落地后是谁赢了&#xff1f; 董宇辉的“小作文事件”“CEO摔手机事件”迎来大结局了&#xff01; 就在12月18日&#xff0c;董宇辉被任命为新东方教育科技集团董事长文化助理&#xff0c;兼任新东方文旅集团副总裁。有朋…

Excel如何将行的值转换为列值?

问题:Excel如何将行的值转换为列值?(如图左表变成右表) 1.用 SUMIFS(求和区域, 条件区域1, 条件1, [条件区域2, 条件2], ...)函数 比如:=SUMIFS($C$2:$C$8,$A$2:$A$8,H3,$B$2:$B$8,"快车") 2.直接用简单的透视表 (1)随机点击目标目标表格任何位置,点击插入…

Gin之GORM事务(转账操作)

禁用默认事务的操作 为了确保数据一致性,GORM 会在事务里执行写入操作(创建、更新、删除)。如果没有这方面的要求,您可以在初始化时禁用它,这将获得大约 30%+ 性能提升。 // 全局禁用 db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{SkipDef…

Just Laws -- 中华人民共和国法律文库,简单便捷的打开方式

链接&#xff1a;JustLaws | Home 一个简洁便捷的中华人民共和国法律文库&#xff0c;而且收录比较完善&#xff0c;都是平常网民可能用到比较多的法律知识&#xff0c;目前包括宪法及宪法相关法、民商法、行政法、经济法、社会法、刑法和程序法等等 页面以文档的风格展示每一…

Java设计模式:工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)

❤ 作者主页&#xff1a;欢迎来到我的技术博客&#x1f60e; ❀ 个人介绍&#xff1a;大家好&#xff0c;本人热衷于Java后端开发&#xff0c;欢迎来交流学习哦&#xff01;(&#xffe3;▽&#xffe3;)~* &#x1f34a; 如果文章对您有帮助&#xff0c;记得关注、点赞、收藏、…

正向代理和反向代理的区别与联系

一、代理 代理就相当于中间商,本来A和B是可以直接连接的,但是此时添加了一个C在中间,A跟B不直接连接,而是通过C作为中介进行连接。最常见的例子就是二手东,其实很多我们租房子时签约的人不是房子的真正房东,而是房东委托的中介,房东不想管事或者房子太多,只靠自己无法进行管理,…

LVS负载均衡器(DR模式)+nginx七层代理+tomcat多实例+php+mysql 实现负载均衡以及动静分离、数据库的调用!!!

目录 前言 一、nfs共享存储&#xff0c;为两个节点服务器提供静态网页共享 二、nginx作为lvs的后端节点服务器&#xff0c;完成lo:0网卡配置&#xff0c;以及内核参数设置&#xff0c;还有设置路由表 步骤一&#xff1a;先完成nfs共享存储挂载 步骤二&#xff1a;完成lo:0网…

七:爬虫-数据解析之正则表达式

七&#xff1a;正则表达式概述 正则表达式&#xff0c;又称规则表达式,&#xff08;Regular Expression&#xff0c;在代码中常简写为regex、regexp或RE&#xff09;&#xff0c;是一种文本模式&#xff0c;包括普通字符&#xff08;例如&#xff0c;a 到 z 之间的字母&#xf…