C++string类常用函数

C++ string类 使用方法(各种常用函数的介绍) 必要性 对于字符与字符串的模拟与各种操作的题目,在各类OI基础赛事中层出不穷。 掌握基础的字符串函数和各类读入、修改、输出等奇奇怪怪的操作技巧非常有必要。 前情提要 在 C 中,字符与数组拥有同类的使用方法,操作直观,浅显易懂,之前的两篇扫盲贴有做简要分析: 1、字符串基础1:字符数组介绍 2、字符串基础2:常用的字符串函数介绍 string 类简介 函数介绍 实例演示 ... 阅读详情

string类的构造函数:
string(const char *s);    //用c字符串s初始化
string(int n,char c);     //用n个字符c初始化
此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常

string类的字符操作:
const char &operator[](int n)const;
const char &at(int n)const;
char &operator[](int n);
char &at(int n);
operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组
const char *c_str()const;//返回一个以null终止的c字符串
int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目

string的特性描述:
int capacity()const;    //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const;    //返回string对象中可存放的最大字符串的长度
int size()const;        //返回当前字符串的大小
int length()const;       //返回当前字符串的长度
bool empty()const;        //当前字符串是否为空
void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分

string类的输入输出操作:
string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'/n'分开。
 

string的赋值:
string &operator=(const string &s);//把字符串s赋给当前字符串
string &assign(const char *s);//用c类型字符串s赋值
string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值
string &assign(const string &s);//把字符串s赋给当前字符串
string &assign(int n,char c);//用n个字符c赋值给当前字符串
string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串
string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
 

string的连接:
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾
string &append(const char *s);            //把c类型字符串s连接到当前字符串结尾
string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾
string &append(const string &s);    //同operator+=()
string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾
string &append(int n,char c);        //在当前字符串结尾添加n个字符c
string &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
 

string的比较:
bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等
运算符">","<",">=","<=","!="均被重载用于字符串的比较;
int compare(const string &s) const;//比较当前字符串和s的大小
int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小
int compare(int pos, int n,const string &s,int pos2,int n2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小
int compare(const char *s) const;
int compare(int pos, int n,const char *s) const;
int compare(int pos, int n,const char *s, int pos2) const;
compare函数在>时返回1,<时返回-1,==时返回0  

string的子串:
string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串


string的交换:
void swap(string &s2);    //交换当前字符串与s2的值

 

string类的查找函数:

int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值

int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置
int rfind(const char *s, int pos = npos) const;
int rfind(const char *s, int pos, int n = npos) const;
int rfind(const string &s,int pos = npos) const;
//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值

int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置
int find_first_of(const char *s, int pos = 0) const;
int find_first_of(const char *s, int pos, int n) const;
int find_first_of(const string &s,int pos = 0) const;
//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。查找失败返回string::npos

int find_first_not_of(char c, int pos = 0) const;
int find_first_not_of(const char *s, int pos = 0) const;
int find_first_not_of(const char *s, int pos,int n) const;
int find_first_not_of(const string &s,int pos = 0) const;
//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::npos

int find_last_of(char c, int pos = npos) const;
int find_last_of(const char *s, int pos = npos) const;
int find_last_of(const char *s, int pos, int n = npos) const;
int find_last_of(const string &s,int pos = npos) const;

int find_last_not_of(char c, int pos = npos) const;
int find_last_not_of(const char *s, int pos = npos) const;
int find_last_not_of(const char *s, int pos,  int n) const;
int find_last_not_of(const string &s,int pos = npos) const;
//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找

 

string类的替换函数:

string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符
string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串s
string &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符
string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符c
string &replace(iterator first0, iterator last0,const char *s);//把[first0,last0)之间的部分替换为字符串s
string &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符
string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串s
string &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符c
string &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串

string类的插入函数:

string &insert(int p0, const char *s);
string &insert(int p0, const char *s, int n);
string &insert(int p0,const string &s);
string &insert(int p0,const string &s, int pos, int n);
//前4个函数在p0位置插入字符串s中pos开始的前n个字符
string &insert(int p0, int n, char c);//此函数在p0处插入n个字符c
iterator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置
void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符
void insert(iterator it, int n, char c);//在it处插入n个字符c
 

string类的删除函数

iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置
iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置
string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串

 

string类的迭代器处理:

string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。
用string::iterator或string::const_iterator声明迭代器变量,const_iterator不允许改变迭代的内容。常用迭代器函数有:
const_iterator begin()const;
iterator begin();                //返回string的起始位置
const_iterator end()const;
iterator end();                    //返回string的最后一个字符后面的位置
const_iterator rbegin()const;
iterator rbegin();                //返回string的最后一个字符的位置
const_iterator rend()const;
iterator rend();                    //返回string第一个字符位置的前面
rbegin和rend用于从后向前的迭代访问,通过设置迭代器string::reverse_iterator,string::const_reverse_iterator实现

 

字符串流处理:

通过定义ostringstream和istringstream变量实现,<sstream>头文件中
例如:
    string input("hello,this is a test");
    istringstream is(input);
    string s1,s2,s3,s4;
    is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
    ostringstream os;
    os<<s1<<s2<<s3<<s4;
    cout<<os.str();

【C++STL基础入门】wstring类基础使用 在C++的标准模板库(STL)中,wstring是一个重要的类,它是string的宽字符版本,用于存储和操作宽字符序列。本文将详细介绍wstring类的基础使用。在C++的标准模板库(STL)中,wstring是一个重要的类,它是string的宽字符版本,用于存储和操作宽字符序列。本文将详细介绍wstring类的基础使用。wstring是C++ STL中的一个重要类,它提供了一系列功能强大的函数,使得处理宽字符序列变得简单方便。理解和掌握wstring的使用,对于编写高效、易读的C++代码至关重要。 阅读详情

相关推荐

CAPL编程的催化剂——CAPLdll的实现

CANoe作为一款功能强大的总线开发及测试工具,不仅仅是因为其丰富的库函数,最重要的是其兼容了多种编程语言,可以实现灵活的开发,极大的解决了纯CAPL编程所带来的限制,如通过动态链接库(DLL)来实现复杂函数的编写,使其能够在CAPL中可以调用,下面将对DLL的实现进行分析。capldll.cpp这个文件初看相当复杂,从整体上来说,这段代码主要包含了两部分:自定义函数实现和回调函数定义,今天的主要任务是实现自定义函数,这里的函数定义与普通函数定义差不多,只是利用了CAPL的格式。在VS2017工程中打开。

车载控制器AutoSAR软件开发工程师 4648

C++string类的介绍及常用函数用法总结

一.strng类的介绍   二.string类常用函数及用法    1.string类对象常见的构造    2.string类对象的容量操作    3. string类对象的访问及遍历操作    4.string类对象的修改操作

2301_77509762的博客 2983

silvaco例子库学习—mos1ex02

mos1ex02相比于第一个例子,主要进行了mos的输出特性仿真,工艺结构是一样的,所以就只简单讨论一下atlas特性仿真的部分。

weixin_61913839的博客 853

C++的string类常用函数

C++的string类常用函数 s.assign() // 对字符串赋以新值 string s = "liulan"; s.assign("niupi"); swap() // 交换两个字符串的内容 string s1 = "liulan"; string s2 = "gmy"; cout << s1 << " " << s2 << endl; s2.swap(s1); cout << s1 &

daoyone的博客 324

F460 CLK配置功能模块说明

F460CLK配置功能模块说明 目录 F460CLK配置功能模块说明 CLK配置: 1. DDL_ICG——初始化配置 2. DDL_UTILITY——基础功能函数 3.DDL_PRINT-待细化 4.DDL_ADC 5.DDL_AES——AES 加解密算法处理器 6.DDL_CAN 7. DDL_CLK 8. DDL_CMP——电压比较器 9. DDL_CRC 10. DDL_DCU——数据计算单元 11. DDL_DMAC——DMA 控制器 ...

childboy的博客 2457

ETL基础概念及要求详解

ETL即Extract(抽取)Transform(转换)Load(装载)的过程,如下图,同时也包括数据清洗的部分,是数据从源端到目标端的一个数据处理的过程,是构建数据仓库的重要一环,但不局限于构建数据仓库其他BI(商业智能)的设计和开发数据分析平台也经常用到ETL。

qq_43605229的博客 8944

C++STRING类常用函数

C++STRING类常用函数 C++string类常用函数 string类的构造函数: string(const char *s);    //用c字符串s初始化 string(int n,char c);     //用n个字符c初始化 此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无

718

11、ThingsBoard集群部署k8s

今天终于有时间去搞这个啦,拖了很久了,一直没时间,因为我本地没有那么多机器资源,开虚拟机不够,如果租用阿里云服务器,需要有充值的时间,因为这个费用是按小时付费,需要有连贯的时间来搞才行,今天恰好有时间,就开始搞了,弄成功搞出来了,特地写博客记录下来。

AldrichEugene 991

【C++】string类常用函数用法总结

const char& operator[] (size_t pos) const,const_iterator begin() const,const_iterator end() const,const修饰*this,即this指针指向的内容,所以this指针指向的内容不可修改,用法上和其他函数是一样的。resize(size_t n)和resize(size_t n, char c)都是将字符串的有效个数改变到n个,不同的是,当使用 std::string::resize(size_t n)

记录代码路上的点滴思考。 1027

设备接入与APP(应用程序)接入华为云iotDA平台的详细操作步骤及获取方式

设备及APP(应用程序) 登录 IoTDA 平台所需的 入场与操作清单内容,提供逐项详细操作步骤和获取方式,并穿插贴士便于理解与实际操作落地。内容基于 华为云 IoTDA 控制台 和 IAM 控制台 的实际使用路径整理。

hy行者勇哥的博客 3059

在 SAP 世界里拥抱非 SAP 技术:一名 ABAP 开发者的成长路径与实战方法论

那段时间非常煎熬,但它带来的回报也很真实:你会更尊重“可观测性”的价值,也会更愿意在 SAP 场景里把日志、追踪、断言、隔离测试做扎实。解决这类问题,靠的并不是某个神奇的 SAP 函数,而是跨栈的工程设计:接口要有业务幂等键,SAP 侧要有落库与锁策略,返回要有可识别的状态码,异常要能回放,补偿要有可追踪的工单或队列。这套体系吸引我的地方,在于它更贴近真实业务:客户要的往往不是“能打印”,而是“能规范地输出”,包括版式、字体、条码、二维码、电子签章位置、分页规则、多语言、合规水印、可回写的交互字段等。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 168

C++ string 类常用成员函数

本文介绍了C++中string类的常用操作,主要包括:1.构造函数的不同用法(空字符串、拷贝构造、重复字符等);2.赋值函数assign()的多种形式;3.容量相关函数(size、capacity等);4.元素访问方法([]、at等);5.字符串修改操作(append、erase等);6.字符串操作函数(find、substr等);7.转换函数(c_str、stoi等)。文章详细说明了每种方法的使用场景和注意事项,并提供了代码示例,最后总结了string类最常用的函数分类表。

ViolEvergarden的博客 1065

Pointnet++改进90:2025最新改进模块-EVA模块

本教程提出了一种改进PointNet++特征提取模块的方法,通过在PointNetSetAbstraction模块中加入NAMAttention注意力机制,显著提升了模型性能。具体改进包括:1)新建eva.py文件,实现了包含SDLSKA、CKS等关键组件的EVA模块;2)在pointnet2_utils.py中添加Conv和PointNetSetAbstractionAttention类,整合注意力机制;3)修改pointnet2_sem_seg.py模型结构。这种改进方法降低了实现难度,同时保持了33F

AIcurator的博客 361

C++string类常用的操作函数

#include &lt;iostream&gt; #include &lt;string&gt;//必须包含该C++头文件 using namespace std; int main() { //string对象的初始化,也就是构造函数与析构函数方面的知识 /* string s; //生成一个空字符串s string s(str); //拷贝构造函数 生成...

Alexander的博客 2356

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

【C++】string类中的常用成员函数介绍

本文介绍了C++中string类的基本概念和使用方法。string类作为处理字符串的核心工具,相比C语言字符数组具有自动内存管理、丰富成员函数和运算符重载等优势。重点讲解了string对象的构造方法、访问与遍历操作(包括[]操作符、迭代器和范围for循环)、容量操作(size/length/capacity等)以及修改操作(插入/删除/查找/子串等)。通过示例代码展示了string类的常用接口,帮助读者理解如何高效安全地处理字符串。string类大大简化了字符串操作,是C++编程中不可或缺的重要工具。

m0_65245374的博客 1164

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

C++中string类常用函数总结

string类的使用 #include<string> using std::string;

DayDayUp 5214

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C++:STL:String类常用函数介绍(附加部分重要函数的模拟实现)

C语言中,字符串是以'\0'结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列 的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户 自己管理,稍不留神可能还会越界访问。如果我们经常在leetcode上刷OJ题,我们会发现字符串类型的题目基本都是以string的形式给初始字符串。cplusplus.com/reference/string/string/?kw=string上面的网址可以帮助我们了解string类,同时需要记得在使用string类的时候需要包

xiuwoaiailixiya的博客 1127

掌握C++ string类的常用函数

本文系统介绍了C++中std::string类的核心功能,包括构造与赋值、容量管理、字符访问、字符串操作等常用成员函数。重点讲解了字符串构造、查找(find)、提取(substr)、修改(insert/erase/replace)等操作,并强调了reserve()和shrink_to_fit()对性能的优化作用。此外还介绍了迭代器接口和流操作,展示了std::string与STL算法的兼容性。掌握这些函数能有效提升字符串处理代码的安全性和效率。

2503_93272531的博客 466

C++中string类的常用函数

引言:在C语言中对于字符串的一些操作,例如在字符串末尾增加字符,对字符串拷贝等,我们可以通过调用库中函数来完成这些操作,在C++中,我们把这些常规操作封装成了string类,可以通过类对象直接调用这些函数,使得更加符合了面向对象的思想。

weixin_61488314的博客 4550

【C++STL】一文总结String类的常用函数用法总结

📝【C++ STL string类使用指南】 本文系统介绍了C++ STL中string类的核心操作: 1️⃣ 初始化:6种构造方式(空串/拷贝/子串/字符填充等) 2️⃣ 访问方式:下标[]、迭代器(正向/反向)、范围for循环 3️⃣ 容量管理:size()/length()、capacity()、reserve()/resize()区别 4️⃣ 常用操作:插入(push_back/insert)、拼接(append/+)、删除(erase/clear)、替换(replace) 。

tangsl1552的博客 1242

【c++】 string类常用的函数

类的核心功能,使 C++ 中的字符串处理变得非常方便和灵活,避免了 C 风格字符串处理中容易出现的缓冲区溢出等问题。类还提供了许多其他的实用函数,例如。等,可以根据具体需求进行使用。

Kevin_Gates的专栏 2269
上一篇: c++ string 的函数replace()用法
下一篇: MultiByteToWideChar和WideCharToMultiByte用法详解
Elecsoft
博客等级 码龄17年 32粉丝 21原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值