C++ -string -常见用法4

news/2024/10/19 8:22:43/

博客主页:【夜泉_ly】
本文专栏:【C++】
欢迎点赞👍收藏⭐关注❤️

在这里插入图片描述

文章目录

  • 💡前言
  • 💡字符串操作
    • 1.c_str 、data -重点⭐
      • 1.1函数原型
      • 1.2用法
    • 2.copy
      • 2.1函数原型
      • 2.2用法
      • 2.3注意事项
    • 3.find、rfind -重点⭐
      • 3.1函数原型
      • 3.2用法
      • 3.3注意事项
    • 4.find_..._of
      • 4.1功能简述
    • 5.substr
      • 5.1函数原型
      • 5.2用法
    • 6.compare
      • 6.1简介
      • 6.2返回值

💡前言

在这篇文章中,我将继续简单探讨 C++string 的基本用法。写这篇文章的主要目的是帮助我巩固所学的知识,同时也便于未来的复习和参考。

如果你想大致的了解 string 的基本用法,也可以留下来看看。

对于那些希望深入学习更多细节的读者,可以去看看这个网站:cplusplus.com,以获取更全面的参考资料。

💡字符串操作

1.c_str 、data -重点⭐

string::datastring::c_str是同义词,返回值完全相同。
一般用c_str

1.1函数原型

const char* c_str() const;

1.2用法

  • 返回C类型的字符串,可以和C语言的一些接口函数配合:
string filename = "test.cpp";
FILE* fout = fopen(filename.c_str(), "r");
  • 这里和string::operator<<做一下对比,可以看出两者的区别:
void Test()
{string str("Hello");str += '\0';str += " World";cout << str << endl;cout << str.c_str() << endl;
}

Output:

Hello World
Hello

2.copy

2.1函数原型

size_t copy (char* s, size_t len, size_t pos = 0) const;

2.2用法

string类对象的内容拷贝到字符串。

string str("Hello World");
char* c = new char[str.size()+1];
str.copy(c, str.size());
c[str.size()] = '\0'; // 这里只能手动加\0
cout << c << endl;

Output:

Hello World

2.3注意事项

  1. 拷贝时不会在拷贝内容之后加’\0’
string str("666");
char c[10];
str.copy(c , str.size());
cout << c;

运行结果如下图:
在这里插入图片描述

  1. 字符数组不够长,行为未定义。
  2. pos 大于 size,抛异常。

3.find、rfind -重点⭐

3.1函数原型

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const;

3.2用法

在字符串中查找指定内容。
其中 find是正向查找,rfind 是反向查找。
find :

void Test()
{string str("Hello World!!!");size_t found = str.find(string("!!"));// 从头找,找 “!!” , 找到了返回对应下标cout << found << endl;found = str.find("!!", found + 2);// 在上次查找的下一个位置查找“!!”, 找不到返回string::npos, 即-1(整型最大值)cout << found << endl;found = str.find("!!!!!", 0, 3);// 从头开始找,找“!!!!!”的前3个,即“!!!”, 找到了返回对应下标cout << found << endl;found = str.find('!');// 从头开始找, 找字符‘!’, 找到了返回对应下标cout << found << endl;
}

Output :

11
4294967295
11
11

反向查找rfind的用法与find基本相似,不过需注意反向查找的pos缺省值是npos,即默认从尾部开始查找。

3.3注意事项

pos过大 或者 n过大,行为未定义。
vs上是直接返回string::npos

void Test()
{string str("666666");size_t found = str.find("666", -1);cout << found << endl;found = str.find("666", 0, -1);cout << found << endl;
}

Output:

4294967295
4294967295

4.find_…_of

这里共有四个函数:
find_first_of
find_last_of
find_first_not_of
find_last_not_of
其函数原型与findrfind类似,可以类比使用。

4.1功能简述

函数功能
find_first_of返回字符串中第一个属于指定字符集合的下标。
find_last_of返回字符串中最后一个属于指定字符集合的下标。
find_first_not_of返回字符串中第一个不属于指定字符集合的下标。
find_last_not_of返回字符串中最后一个不属于指定字符集合的下标。

在这里插入图片描述

5.substr

5.1函数原型

string substr (size_t pos = 0, size_t len = npos) const;

5.2用法

pos位置开始,搞出一个长度为len的字串。
截取 [pos,len) 位置的。

void Test()
{string str("Hello World");string str1 = str.substr();string str2 = str.substr(str.find('W'));string str3 = str.substr(0, str.find('W'));cout << "str1: " << str1 << endl;cout << "str2: " << str2 << endl;cout << "str3: " << str3 << endl;
}

Output:

str1: Hello World
str2: World
str3: Hello

6.compare

6.1简介

提供了多种方式进行字符串的比较:
在这里插入图片描述
这里面用的最多的感觉是第一个:

void Test()
{string str("Hello World");string str1 = str.substr();string str2 = str.substr(str.find('W'));string str3 = str.substr(0, str.find('W'));cout << str.compare(str1) << endl;cout << str.compare(str2) << endl;cout << str.compare(str3) << endl;
}

Output :

0
-1
1

6.2返回值

返回值意义
0相等
<0比较字符串中的第一个不匹配字符的值较小,或者所有比较的字符匹配但比较字符串较短。
>0比较字符串中的第一个不匹配字符的值较大,或者所有比较的字符匹配但比较字符串较长。


在这里插入图片描述


希望本篇文章对你有所帮助!并激发你进一步探索编程的兴趣!
本人仅是个C语言初学者,如果你有任何疑问或建议,欢迎随时留言讨论!让我们一起学习,共同进步!


http://www.ppmy.cn/news/1540217.html

相关文章

Java | Leetcode Java题解之第493题翻转对

题目&#xff1a; 题解&#xff1a; class Solution {public int reversePairs(int[] nums) {Set<Long> allNumbers new TreeSet<Long>();for (int x : nums) {allNumbers.add((long) x);allNumbers.add((long) x * 2);}// 利用哈希表进行离散化Map<Long, Int…

基于vue框架的的大学校园社团管理系统q00q2(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。

系统程序文件列表 项目功能&#xff1a;学生,社团管理员,社团介绍,加入社团,社团活动,参加活动,社团新闻,社团成员 开题报告内容 基于Vue框架的大学校园社团管理系统开题报告 一、项目背景与意义 随着高等教育的普及和大学生对多样化、个性化需求的增加&#xff0c;大学校园…

springboot web 和webflux两个都引用会怎样?

前一篇发了 springboot 启动 Check your application‘s dependencies for a supported reactive web server-CSDN博客 虽然是解决了&#xff0c;但还是要一探究竟 原因&#xff1a; 在我的项目里引用了pom.xml 引入了 spring.boot.parent 此时如果直接写SpringBootApplicat…

MySQL数据库增删改查基础操作(超长详解)

目录 1库的操作 显示数据库&#xff1a; 创建一个库 使用数据库 删除数据库的名 2表操作&#xff1a; 显示表 创建表 查看表 删除表名 新增 查出表的所有行和列&#xff1b; 实例&#xff1a; 别名&#xff1a; 去重&#xff1a; 排序&#xff1a; 限制查找的…

第一百零七周周报

学习时间&#xff1a; 2024.10.12-2024.10.18 学习产出&#xff1a; 这周大部分时间都在黄山开会&#xff0c;目前cifar10还没调好&#xff0c;celebA128的fid到了13点多&#xff0c;还没有跑完&#xff0c;其他时间都在找工作。

基于Flink+Hologres搭建实时数仓

Apache Paimon是一种流批统一的数据湖存储格式&#xff0c;结合Flink及Spark构建流批处理的实时湖仓一体架构。Paimon创新地将湖格式与LSM技术结合起来&#xff0c;给数据湖带来了实时流更新以及完整的流处理能力。借助实时计算Flink版与Apache Paimon&#xff0c;可以快速地在…

矩阵相关算法

矩阵旋转90度 给定一个 n n 的二维矩阵 matrix 表示一个图像&#xff0c;请你将图像顺时针旋转 90 度。 #include <iostream> #include <vector>using namespace std;void rotate(vector<vector<int>>& matrix) {int n matrix.size();// 第一步…

侏罗纪公园不再是电影了吗?

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…