当前位置: 首页 > news >正文

C++ -string -常见用法4

博客主页:【夜泉_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.mrgr.cn/news/53534.html

相关文章:

  • Ruby CGI Cookie
  • 骑砍战团MOD天芒传奇-多人联机
  • 阿里巴巴达摩院|Chain of Ideas: 利用大型语言模型代理革新新颖创意开发的研究
  • Openlayers高级交互(3/20):动态添加 layer 到 layerGroup,并动态删除
  • 浅谈华为 HarmonyOS Next
  • ChatGPT的150个角色提示场景实测(18)个人造型师
  • 三周精通FastAPI:2 路径参数以及声明路径参数的类型
  • 读数据工程之道:设计和构建健壮的数据系统13无服务器
  • UNION 联合查询
  • 滞后对数收益率
  • 代码随想录day42:单调栈part2
  • 华为HCIP-openEuler认证详解
  • 【Python】为什么不能直接比较数字 if student_id == 667788
  • 如何将两个同样大小的List组装成一个Map?
  • windows C++-有效使用PPL(四)
  • Golang | Leetcode Golang题解之第492题构造矩形
  • 华为OD机试2024年真题( 最远足迹)
  • Python库matplotlib之十一
  • manimgl 安装win7
  • Vue脚手架学习 vue脚手架配置代理、插槽、Vuex使用、路由、ElementUi插件库的使用