std::string用法总结

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

在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询。在网上摘抄一下总结一下,为以后的查询方便:

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 用法详解 阅读详情

相关推荐

EGO-Planner文章解读(一)——论文原理和算法实现

详细解读egoplanner论文和代码

fegxg的博客 4007

std::string详解

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

wuguangbin1230的博客 5万+

基于集成运放心电放大器设计(原创)

原创作者:einyboy or alert 最近在看美剧《危机边缘》关于边缘科学的案件,科技控可以看一下,但建议不在吃东西的时候看。想像力是创造的思想源头,动手能力则是创造的车床。 自认为是没有想像力的那类,只能发挥动手能力了。 心电测量简介 图1 心电极接线图 如图1心电测试一般使用3个心电极进行测量,各个电极的说明如下: RA: Right Arm缩写,右手电极 LA: L...

huandaohack的专栏 6936

C++ 基础: std::string

std::string 快速上手

Mr.Sugarcane 1万+

从理论到实践:全面解析Cmk(设备能力指数)的应用与优化

本文全面解析了设备能力指数Cmk的核心概念、计算逻辑与关键应用场景。Cmk作为衡量设备本身固有加工能力的黄金标准,在设备验收、大修后验证及过程能力根因分析中至关重要。文章通过实战案例,详细阐述了执行Cmk测试的“五大固定”黄金法则、数据计算分析方法,并提供了Cmk不达标时的系统性排查与优化策略,帮助制造企业实现预防性质量管理。

jump7的博客 557

【论文汇总】Diffusion Models视频生成/视频编辑/可控视频生成/跨模态视频生成

本文总结了Diffusion Models视频生成领域相关的工作,目前共收录142篇,持续更新中。

沉迷单车的追风少年 1681

【C++基础】std::string详解

std::string 是 C++ 标准库提供的用于处理字符串的类。它在 头文件中定义。std::string 提供了一种灵活、高效的字符串表示方式,相比于 C 语言中的字符串表示(使用字符数组或指针),std::string 更易于使用,更安全,并且提供了许多便捷的操作。

2201_75772333的博客 1万+

Flink CDC 中 StartupOptions 模式详解

Flink CDC 的 StartupOptions 模式用于指定作业启动时从源数据读取数据的起始位置,不同的模式适用于不同的业务场景,可满足多样化的数据处理需求,主要有 Initial 模式、Earliest 模式、Latest 模式、SpecificOffset 模式和 Timestamp 模式。

RodJohnsonDoctor的专栏 856

C/C++笔记:std::string(一)

1. 需求:把两个字符串拼接一下. memcpy参考【C/C++笔记:动态内存分配】 char strA[0x10] = "123"; char strB[0x10] = "456"; char strC[0x20]; memcpy(strC, strA, strlen(strA)); memcpy(strC + strlen(strA), strB, strlen(strB) + 1); std::cout << strC << std::endl; ..

南城同学的博客 1万+

最新Python爬虫实战(入门爬虫篇)——案例1:某瓣网站Top250图片数据采集(附上完整爬虫代码)

本次案例通过Python爬虫技术实现了某瓣Top250电影封面的批量下载。代码采用模块化设计,函数实现cookies的自动解析,简化了配置流程,涵盖请求、解析、存储的完整流程。学习重点包括:抓包工具定位数据源、正则表达式匹配图片链接、模拟请求头绕过反爬机制、以及自动化文件管理。需要注意控制请求频率并遵守网站使用规范,适用于入门爬虫实践。

努力让自己发光,对的人才能迎着光而来 16万+

最新Python爬虫实战(入门爬虫篇)——案例8:图书网畅销榜数据采集(详细爬虫思路截图+抓包动图演示+完整爬虫代码+详细注释)

本次案例通过 Python 爬虫技术,成功实现了对图书网畅销榜多页数据的批量爬取与 Excel 保存。代码采用模块化设计,包含分页参数构造、API 接口请求、JSON 数据解析、Excel 数据持久化等完整流程;通过配置模拟浏览器的请求头,有效规避基础反爬策略,保证请求的稳定性。整个爬虫逻辑遵循 “分页遍历 → 接口请求 → JSON 解析 → 提取图书信息 → 保存到 Excel” 的标准流程,与电商类接口爬取范式高度一致。

努力让自己发光,对的人才能迎着光而来 29万+

什么是std::string...?怎么用

std::string是标准C++的字符串实现。为了让程序好移植,要用std::string。比如: 方法1: #include<string> std::string 方法2: #include<string> usingnamespacestd; s...

测试0901-1 415

基于YOLOv8的端面小目标计数解决方案: 多头检测器+小缺陷到大缺陷一网打尽的GiraffeDet+Wasserstein Distance Loss | 助力工业小目标缺陷检测

端面小目标计数部署解决方案 | 亲测在工业小目标缺陷涨点明显,原始mAP@0.5 0.936提升至0.955

①订阅者答疑群聊服务;②FDE:工业缺陷检测项目;③paper/创新算法指导;④免费提供YOLO创新涨点推荐顾问Skill和AI小怪兽数字分身; 1963

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

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

Taylor_0522的博客 1819

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

C++ std::string erase用法

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

guoke16888的专栏 871

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

std::string的用法总结

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

千里之行,始于足下 1259

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

上一篇: 关于三极管
下一篇: 有关wince下校准界面的修改
lanyzh0909
博客等级 码龄18年 105粉丝 54原创
评论 8
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值