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

news2024/9/22 1:05:15

各位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模拟实现,加油!!!

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

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

相关文章

硬件基础-电感

电感 目录 1.原理 2.作用 3.高频等效模型 4. 直流偏置特性 5.器件选型 6.电感损耗 7.功率电感 8.贴片电感 9.共模电感 10.差模电感 1.原理 电感是阻碍电流的变化,储能 电感的磁芯决定了电感的饱和电流&#xff0c;也决定了电感值与电流的变化曲线&#xff0c;磁滞损…

Leetcode—77.组合【中等】

2023每日刷题&#xff08;六十五&#xff09; Leetcode—77.组合 算法思想 实现代码 class Solution { public:vector<vector<int>> combine(int n, int k) {vector<vector<int>> ans;vector<int> path;function<void(int)> dfs [&…

linux 驱动——杂项设备驱动

杂项设备驱动 在 linux 中&#xff0c;将无法归类的设备定义为杂项设备。 相对于字符设备来说&#xff0c;杂项设备的主设备号固定为 10&#xff0c;而字符设备不管是动态分配还是静态分配设备号&#xff0c;都会消耗一个主设备号&#xff0c;比较浪费主设备号。 杂项设备会自…

PyCharm添加自动函数文档注释

目录 1、背景2、开启PyCharm自动函数文档注释 1、背景 规范的函数文档注释有助于他人理解代码&#xff0c;便于团队协作、提高效率。但如果我们自己手写函数文档注释将非常耗时耗力。PyCharm安装后默认没有开启自动化函数文档注释&#xff0c;需要我们开启 2、开启PyCharm自动…

JavaWeb笔记之前端开发JavaScript

一、引言 1.1 简介 JavaScript一种解释性脚本语言&#xff0c;是一种动态类型、弱类型、基于原型继承的语言&#xff0c;内置支持类型。 它的解释器被称为JavaScript引擎&#xff0c;作为浏览器的一部分&#xff0c;广泛用于客户端的脚本语言&#xff0c;用来给HTML网页增加…

LVM Sequential Modeling Enables Scalable Learning for Large Vision Models

LVM: Sequential Modeling Enables Scalable Learning for Large Vision Models TL; DR&#xff1a;本文提出一种纯视觉的序列建模方法 LVM&#xff0c;不需要任何文本数据。通过 visual sentences 的形式&#xff0c;统一图像/视频/标注/3D数据&#xff0c;使用 VQGAN 将视觉…

网上平台交易伦敦金靠谱吗?

随着科技的发展&#xff0c;网络交易已经成为了我们生活中的一部分。在金融领域&#xff0c;许多投资者也早已开始转向在线交易平台进行投资交易&#xff0c;其中就包括伦敦金。然而&#xff0c;面对众多的网上交易平台&#xff0c;投资者们往往会产生这样的疑问&#xff1a;“…

4.3 C++对象模型和this指针

4.3 C对象模型和this指针 4.3.1 成员变量和成员函数分开存储 在C中&#xff0c;类内的成员变量和成员函数分开存储 只有非静态成员变量才属于类的对象上 #include <iostream>class Person { public:Person() {mA 0;} //非静态成员变量占对象空间int mA;//静态成员变量…

【spark】spark内核调度(重点理解)

目录 spark内核调度DAGDAG的宽窄依赖和阶段划分内存迭代计算面试题Spark是怎样做内存计算的&#xff1f;DAG的作用是什么&#xff1f;Stage阶段划分的作用&#xff1f;Spark为什么比MapReduce快 spark并行度如何设置并行度&#xff1a;spark.default.parallelism集群中如何规划…

P1 H264码流结构分析 (上)

目录 前言 01 什么是码流结构 02 H264帧类型的区别 03 片slice 前言 从本章开始我们将要学习嵌入式音视频的学习了 &#xff0c;使用的瑞芯微的开发板 &#x1f3ac; 个人主页&#xff1a;ChenPi &#x1f43b;推荐专栏1: 《C_ChenPi的博客-CSDN博客》✨✨✨ &#x1f525…

官方指定Jmeter配置JVM堆内存方式

1.概述 在使用Jmeter做性能测试过程中&#xff0c;可能会应为默认设置的堆内存值较小出现堆内存溢出问题&#xff0c;此时解决的方式有两种&#xff0c;分布式测试和调大堆内存。下面介绍官方推荐调整堆内存方法。 2.调整Jmeter堆内存 2.1.介绍官方推荐堆内存调整方法(jmete…

IDEA Community html文件里的script标签没有syntax highlighting的解决方案

在网上找到的解决方法有的是针对Ultimate版本才可以下载的plugin&#xff0c;对我所用的Community版本无法生效&#xff0c;找了一圈最后在stackoverflow上找到一个有效的方案&#xff0c;给需要的小伙伴分享一下&#xff1a;IntelliJ Community Edition: Javascript syntax hi…

Jenkins 执行远程脚本的插件—SSH2 Easy

SSH2 Easy 是什么&#xff1f; SSH2 Easy 是一个 Jenkins 插件&#xff0c;它用于在 Jenkins 构建过程中通过 SSH2 协议与远程服务器进行交互。通过该插件&#xff0c;用户可以在 Jenkins 的构建过程中执行远程命令、上传或下载文件、管理远程服务器等操作。 以下是 SSH2 Eas…

7-4 JAVA-水仙花数(Java for PTA)

水仙花数是指一个N位正整数&#xff08;7≥N≥3&#xff09;&#xff0c;它的每个位上的数字的N次幂之和等于它本身。例如&#xff1a;153135333。 要求编写程序&#xff0c;计算所有N位水仙花数。 输入格式: 输入一个正整数N&#xff08;3≤N≤7&#xff09;。 输出格式: …

java并发编程四 Monitor 概念,api介绍与线程状态转换

Monitor 概念 Java 对象头 以 32 位虚拟机为例子&#xff1a; 普通对象 数组对象 其中 Mark Word 结构为 64 位虚拟机 Mark Word 小故事 故事角色 老王 - JVM小南 - 线程小女 - 线程房间 - 对象房间门上 - 防盗锁 - Monitor房间门上 - 小南书包 - 轻量级锁房间门上 -…

Elasticsearch常见面试题

文章目录 1.简单介绍下ES&#xff1f;2.简单介绍当前可以下载的ES稳定版本&#xff1f;3.安装ES前需要安装哪种软件&#xff1f;4.请介绍启动ES服务的步骤&#xff1f;5.ES中的倒排索引是什么&#xff1f;6. ES是如何实现master选举的&#xff1f;7. 如何解决ES集群的脑裂问题8…

本地文件内容搜索神器AnyTXT Searcher如何搭建与远程访问

文章目录 前言1. AnyTXT Searcher1.1 下载安装AnyTXT Searcher 2. 下载安装注册cpolar3. AnyTXT Searcher设置和操作3.1 AnyTXT结合cpolar—公网访问搜索神器3.2 公网访问测试 4. 固定连接公网地址 前言 你是否遇到过这种情况&#xff0c;异地办公或者不在公司&#xff0c;想找…

ubuntu qt 源码编译

官方源码下载地址 : 源码地址 选择要下载的版本 dmg结尾的是MacOS系统里使用的Qt库&#xff0c;qt-everywhere-opensource-src-4.7.0是Qt源码包&#xff0c;有zip和tar.gz两个压缩格式的&#xff0c;两个内容是一样的&#xff0c;只是zip一般在Windows下比较流行&#xff0c;…

【100个Cocos实例】快要圣诞节了,给大家支个招!

引言 Mask遮罩组件的一些简单实例 在游戏开发中常常需要在UI界面上展示玩家的头像或者Logo&#xff0c;通常都会是正方形。 偶尔也会有一些奇形怪状的需求&#xff0c;例如五边形、六边形、心形等等。 本文将介绍一下在Cocos游戏开发中Mask遮罩组件的一些简单实例&#xff…

九州未来向开放原子开源基金会捐赠OpenV2X,共建繁荣开源生态

12月16日&#xff0c;以“一切为了开发者”为主题的开放原子开发者大会在无锡成功举办。会上&#xff0c;九州未来将OpenV2X车路协同开源项目正式捐赠给开放原子开源基金会&#xff0c;并签署项目捐赠协议&#xff0c;通过开源共创的方式&#xff0c;携手开源伙伴共同打造车路协…