std::string用法总结

C++中std::string用法详解 本文介绍了Linux C++开发中std::string的核心用法,包括初始化、遍历、拼接、查找替换、截取删除等高频操作,并提供了配置文件解析的实战示例。std::string相比C风格字符串更安全高效,能自动管理内存,避免越界和泄漏问题。文章重点讲解了常见易错点,如边界检查、返回值判断等,并给出可直接运行的代码示例。通过掌握这些基础用法,可以满足80%的日常开发需求,同时为进一步学习字符串转换、比较和性能优化打下基础。 阅读详情


string类的构造函数:
string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化


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中对象字符的处理,有很多已有的函数在CCtype头文件中,可以很方便的应用


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++库字符串类std::string的用法

#include<string>
std::string s1;
std::string s3(s2);
std::string s2("this is a string");
begin       得到指向字符串开头的Iterator
end       得到指向字符串结尾的Iterator
rbegin       得到指向反向字符串开头的Iterator
rend       得到指向反向字符串结尾的Iterator
size       得到字符串的大小
length()       和size函数功能相同
max_size       字符串可能的最大大小
capacity       在不重新分配内存的情况下,字符串可能的大小
empty       判断是否为空
operator[]       取第几个元素,相当于数组
c_str       取得C风格的const char* 字符串
data       取得字符串内容地址
operator=       赋值操作符
reserve       预留空间
swap       交换函数
insert       插入字符
append       追加字符
push_back       追加字符
erase       删除字符串
clear       清空字符容器中所有内容
resize       重新分配空间
assign       和赋值操作符一样
replace       替代
copy       字符串到空间
find       查找,返回基于0的索引号
rfind       反向查找
find_first_of       查找包含子串中的任何字符,返回第一个位置
find_first_not_of       查找不包含子串中的任何字符,返回第一个位置
find_last_of       查找包含子串中的任何字符,返回最后一个位置
find_last_not_of       查找不包含子串中的任何字符,返回最后一个位置
substr(n1,len)       得到字符串从n1开始的长度为len的子串
比较字符串(支持所有的关系运算符)
compare       比较字符串
operator+       字符串链接
operator+=       += 操作符
operator==       判断是否相等
operator!=       判断是否不等于
operator<       判断是否小于
operator>>       从输入流中读入字符串
operator<<       字符串写入输出流
getline       从输入流中读入一行
【C++】:详解 std::string::assign 用法 C++ std::string::assign 用法详解 阅读详情

相关推荐

C++ STL教程(24)std::string用法大全

使用正则表达式匹配结果\n然后将匹配的字符串替换为"" (空字符串)。

二进制君 1547

C++之new 和 delete

new 和 delete要成对使用,且要采取相同形式。 使用new时,会发生2件事,1.内存被分配出来;2.针对此内存会有一个(更多)构造函数被调用; 使用delete,发生2件事,1.针对此内存的析构函数被调用;2.内存被释放; 举例: std::string * stringPtr1 = new std::string; std::string * stringPtr2 =

w1012747007的专栏 491

手把手教你用PLECS仿真BUCK数字电源(含DSP代码实现)

本文详细介绍了如何利用PLECS仿真软件与Visual Studio 2022搭建高保真数字电源开发环境,手把手指导从BUCK电路建模、编写硬件感知的C控制算法到参数调优的全过程。重点阐述了通过DLL接口实现软件在环仿真,模拟DSP的ADC/PWM时序,并将验证后的代码无缝迁移至TI C2000系列DSP,构建从仿真到固件的完整数字电源开发闭环,显著提升开发效率与一次成功率。

oo7890的博客 396

C++ 标准程序库std::string 详解

现在一般不再使用传统的char*而选用C++标准程序库中的string类,是因为string标准程序和char*比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,集成的操作函数足以完成大多数情况下(甚至是100%)的需要。比如,可以用 = 进行赋值操作,== 进行比较,+ 做串联。尽可以把它看成是C++的基本数据类型。C++是将面向过程的C语言和面向对象的Java语言拼接到一起而组成的一种高级语言。 标准模板库(STL)提供了一个std::string类,其是std::basic_str

CHYabc123456hh的博客 3万+

Python 爬取新浪财经 7x24(1): 下载数据

前言 最近在做一个需要很多财经数据的项目。于是想到可不可以通过爬虫来解决(尝试白嫖)。 提供财经数据的网站有很多个,其中比较靠谱和更新比较及时的应该是新浪7x24 live数据。 http://finance.sina.com.cn/7x24/?tag=0 这个问题有一定的难度,因为这个网页是动态加载的。每一次只会加载部分数据,而只有拉到底部才可以加载新的数据。 于是乎,我们要做的事情是: 下载数据 保存到数据库 本篇介绍如何下载数据。 代码 闲言少叙书归正文,代码的实现如下: import reque

ZJ_11701的博客 2905

OpencvSharp学习笔记(一):图片的打开,保存灰度化,保存

近来萌生了学习OpenCv的想法,经过度娘得知老夫会的C#不能直接用OpenCv,只能用他的儿子OpenCvSharp与EmguCV,奈何奈何,这两玩意的资料少的出奇,只能慢慢的摸索了,EmguCV先不说,老夫找到了牛批的资料,先摸索OpencvSharp吧 首先打开VS通过Nuget安装 选择版本后点击安装就OK了. 安装完成后,引用里面就有以下的玩意儿了… 废话不多说了,在写程序前先 using OpenCvSharp 图片打开,灰度化,保存代码如下: static void Main(strin

猥琐de专注 7188

std::string详解

之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。 标准模板库(STL)提供了一个std::strin

debugconsole的专栏 1万+

从代码到实践:手把手教你用EmbodiedOcc++实现高效3D占用预测(含ScanNet配置)

本文提供了EmbodiedOcc++框架在ScanNet数据集上实现高效3D占用预测的完整实践指南。从环境搭建、数据预处理入手,详细解析了其核心创新点——几何引导优化(GRM)与语义感知不确定性采样(SUS)的代码实现与配置,并分享了训练、评估及调参的实战技巧,帮助开发者快速掌握这一先进的3D感知技术。

terraform7cloud的博客 588

C++中std::string常见用法

网上查询到的C++的std::string字符串类常见的用法,摘抄过来作为记录,亦作共勉

Taylor_0522的博客 1819

A2A 协议详解:Agent Card 的设计与实现

Agent Card 是 Agent2Agent (A2A) 协议中实现智能体发现与互操作性的核心组件。本文将深入剖析 Agent Card 的设计理念、关键字段及其在智能体生态系统中的作用。我们将通过详细的 JSON Schema 解析和 Python SDK 示例,帮助您理解如何精确地定义和发布智能体能力,为构建可发现、可协作的 AI 智能体奠定基础。当您使用a2a.Agent类初始化一个智能体时,SDK 会根据您提供的参数(nameversion等)自动构建内部的 Agent Card 表示。

加入“Super Entity”,与全能开发团队共探AI智能体与数字人项目,开启前沿技术之旅。 1476

Oracle EBS Interface/API(49)- AP付款取消API

Oracle EBS AP付款取消API介绍和简单实例

陈显平的专栏-Oracle EBS 850

C++ std::string erase用法

删除first和last之间的字符(包含first,last)删除从pos位置开始,len长度的字符(包含pos)删除从pos位置开始的所有字符(包含pos)

guoke16888的专栏 869

RK3588部署YOLOv11与RKNN全流程教程

本文详细介绍了在RK3588开发板上部署YOLOv11目标检测模型的全流程。主要内容包括:PC端CUDA/cuDNN环境配置、Anaconda虚拟环境搭建、RKNN Toolkit2安装;YOLOv11模型从PyTorch到ONNX再到RKNN的格式转换;以及RK3588开发板的系统配置、RKNPU驱动安装和最终推理实现。教程提供了完整的代码示例,帮助开发者快速在边缘设备上实现高效的AI推理应用,涵盖从环境准备到实时检测的全套解决方案。

FJN110的博客 375

std::string的用法总结

在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询。在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(const char *s);    //用c字符串s初始化string(int n,char c);     //用n个字符c初始化 string类的字符操作: const char &operator[](int

千里之行,始于足下 1256

电化学-论文分享-NanoStat: An open source, fully wireless potentiostat

本文主要分享论文NanoStat: An open source, fully wireless potentiostat,并记录学习笔记。如果有条件,建议看原文最好。

分享技术笔记,希望对大家能够有一定的帮助,同时也通过归纳总结提升自己的个人能力。 2874

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

std::string的用法

本文主要针对那些有C语言背景知识,而现在开始使用C++语言编程的程序员。事实上,C++继承了大多数C语言的功能,但有些方面还是不得不要留意的,如new和delete取代了malloc和free,且C++还使用了STL容器类来静态或动态地分配数组。本文中要讲的是用std::string来取代char*,将会演示C风格数组带来的一系列问题,及如何使用std::string来避免这些问题。避免“病态”的

菜鸟的专栏 2万+

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

上一篇: mysqldump参数大全
下一篇: xtrabackup备份恢复测试
sofia1217
博客等级 码龄17年 36粉丝 24原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值