string类及一些常用接口

2024-04-17 08:38
文章标签 接口 string 常用 类及

本文主要是介绍string类及一些常用接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

string中的常用构造方法
  1. string(); 构造一个空字符串。
  2. string(const char* s);用C风格字符串构造string类的对象
  3. string(size_t n,char c);构造一个含有n个字符c的字符串。
  4. string(const string& s);拷贝构造方法。
//构造方法
void Teststring1() {string s;string s1("hello world");cout << s1 << endl;const char* p = "hello world";string s2(p);cout << s2 << endl;string s3(s1);cout << s3 << endl;string s4(10, 'A');cout << s4 << endl;
}
遍历的常用方法
  1. 下标访问操作符
void Teststring2() {string s1("hello");for (size_t i = 0; i < s1.size(); i++) {cout << s1[i];}cout << endl;
}
  1. 迭代器
void Teststring3() {//正向迭代器string s1("hello");string::iterator it = s1.begin();while (it != s1.end()) {cout << *it;it++;}cout << endl;//反向迭代器string::reverse_iterator rit = s1.rbegin();while (rit != s1.rend()){cout << *rit;++rit;}cout << endl;
}
  1. 范围for
void Teststring4() {//范围forstring s1("hello");for (auto e : s1) {cout << e;}cout << endl;
}
string中对容量操作的常用方法
  1. size_t size() const返回字符串的有效长度.
  2. size_t length() const也是返回字符串的有效字符长度
  3. size_t capacity()const返回的是对象总容量的大小
  4. bool empt()const检测字符串是否为空
  5. void clear()清空字符串中的有效字符
  6. void resize(size_t n , char c)将字符串中的有效字符改成n个,多余的用字符c填充,也可以不传默认用\0填充。
  7. void reverse(size_t n = 0)将字符串容量大小调整到n
void Teststring5() {string s1("hello");cout << s1.size() << endl;cout << s1.capacity() << endl;if (s1.empty()) {cout << "空"<< endl;}else {cout << "不空"<< endl;}s1.clear();//size被清空 capacity没变cout << s1.size() << endl;cout << s1.capacity() << endl;
}
void Teststring6() {string s1("hello world");cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;	s1.resize(20, '!');cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.resize(60, '!');cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;
}

容量不够时会自动扩容,不同编译器内部扩容规则不太一样,容量够不会改变capacity的大小。

void Teststring7() {string s1("hello");cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.reserve(10);cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.reserve(20);cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;s1.reserve(100);cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;//15s1.reserve(15);cout << s1 << endl;cout << s1.size() << endl;cout << s1.capacity() << endl;cout << sizeof(string) << endl;
}

接收的参数大于内部的容量时会扩容,如果是缩减时,当参数大于15时,内部容量不会改变,当参数小于等于15时,容量缩小为15。(vs2019)

string改变字符串内容的常用方法
  1. void push_back (char c);在末尾插入字符c

  2. string& append (const string& str);
    string& append (const char* s);
    string& append (const char* s, size_t n);
    string& append (size_t n, char c);
    在字符串末尾追加字符或者字符串

  3. operator+= 在字符串末尾追加字符或者字符串

  4. size_t find (const string& str, size_t pos = 0) const;
    size_t find (char c, size_t pos = 0) const从pos
    从pos位置向后查找字符或者字符串,返回在原字符串中的位置。

  5. size_t rfind (const string& str, size_t pos = npos) const;
    size_t rfind (char c, size_t pos = npos) const
    从pos位置向前查找字符或者字符串,返回在原字符串中的位置。

  6. string substr (size_t pos = 0, size_t len = npos) const;
    从pos位置开始截取n个字符返回截取到的字符串。

  7. string& erase (size_t pos = 0, size_t len = npos);
    从pos位置开始删除多长的元素。

void Teststring8() {string s1("hello");s1 += 'c';cout << s1 << endl;s1 += "world";cout << s1 << endl;s1.append(3,'!');cout << s1 << endl;string s2("world");s2 = s2.append(s2, 1, 3);cout << s2 << endl;string s4("hello");
}
//insert
void Teststring9() {string s("world");s.insert(0, "hello");cout << s << endl;s.insert(4, 1, ' ');cout << s << endl;
}
//erase
void Teststring10() {string s("hello");s.erase(0,1);cout << s << endl;s.erase(s.begin());cout << s << endl;s.erase(s.begin(), s.end());cout << s << endl;
}
void Teststring11()
{string s("12345");//c风格字符串int value = atoi(s.c_str());cout << value << endl;
}
void Teststring12() {string s("lol.exe");string suffix = s.substr(s.find('.')+1);cout << suffix << endl;string s1("F:\\Game\\lol.exe");size_t pos1 = s1.rfind('\\')+1;size_t pos2 = s1.rfind('.')-1;string filename = s1.substr(pos1, pos2-pos1);cout << filename << endl;
}
void Teststring13() {string s1("abcdefgh");size_t n = s1.find("efg", 0);cout << n << endl;
}

这篇关于string类及一些常用接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/911314

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

SpringBoot+Redis防止接口重复提交问题

《SpringBoot+Redis防止接口重复提交问题》:本文主要介绍SpringBoot+Redis防止接口重复提交问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不... 目录前言实现思路代码示例测试总结前言在项目的使用使用过程中,经常会出现某些操作在短时间内频繁提交。例

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用