C++中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个字符,返回修改后的字符串

void clear(); //清空字符串


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();


REF:

1, http://hi.baidu.com/shazi129/item/d24699affd671916a8cfb788

渗透测试之哥斯拉实战 可以直接在这里使用meterpreter命令进行对服务端的控制。选择目标 - 添加,配置连接信息,之后测试连接成功!php的shell功能如图。先要进行数据库的配置信息。可以直接在这里打包全站。 阅读详情

相关推荐

Nornir自动化简单介绍-网络设备信息收集

作为网工,学会通过执行脚本,可以自动化的完成日常一些设备相关配置或信息收集等工作,可以说是事半功倍。 Nornir就是这样一个自动化网络运维工具。按照官网上的说明: Nornir是用python编写的自动化框架,可与python一起使用。Nornir旨在通过提供一个纯Python框架来解决这些问题。 详细参考: Github GitHub - nornir-automation/nornir: Pluggable multi-threaded framework with inventory .

Gun&rose 7804

string类及其成员函数总结

string类及其成员函数总结

qq_31112171的博客 1621

从VBA到Python:探索CODE V API在MTF数据分析中的跨语言实践

本文详细介绍了如何从VBA转向Python,利用CODE V API实现MTF数据分析的高效工作流。通过Python的科学计算库和现代API设计,光学工程师可以大幅提升数据处理和可视化效率,实现自动化报告生成和性能优化。

weixin_29239661的博客 441

C++STL容器——string成员函数大全(超详细)

Copy//删除串中最后一个字符。

Gefangenes的博客 3161

matlab kwave模拟超声换能器学习笔记

kwave模拟超声换能器的学习笔记,从零开始造车轮

SolvingMaster的博客 3197

BurpSuit在不同浏览器中配置代理

文章通过分享各大浏览器如何设置代理的知识,帮助初学者快速掌握代理配置相关技能。

北冥同学的成长记录笔记 1万+

string类的常用成员函数

1、与字符串对象输入输出相关的函数 (1)从流中读取一个string对象 cin.operator>>(string& str)//cin 中重载的>>的符号函数,有接收字符串的版本 /* 函数功能: 从流中读取一个字符串到字符对象中。 */ //使用举例 string stringObject; cin >> stringObject; (2...

江南又一春 3743

UE5 GAS 学习笔记 1.2游戏标签

这一节对GameplayTag游戏标签进行详细的讲解。

CloudHuCoding 1927

string类的成员函数

1、swap(s1,s2) —— 交换两个字符串的内容 2、s.length() 和 s.size( ) —— 都表示返回字符的数量 3、s.substr(pos) —— 返回 pos 之后的那半边字符串 4、s.substr(a,pos) ——返回 a 到 pos 之间的字符串 5、+= —— 表示两个字符串合并在一起 ...

xiebs的博客 2332

C# 上位机内存泄漏排查指南:3 个工具 + 4 个技巧,从 GC 频繁到稳定运行 720 小时

C# 上位机内存泄漏排查的核心是「先定位泄漏存在,再追溯引用根源,最后修复验证通过「Visual Studio 诊断工具(快速定位)+ dotMemory(深度分析)+ PerfView(生产监控)」三个工具的配合,结合「复现场景、排查静态成员+事件、检查非托管资源、GC 日志验证」四个技巧,可高效解决绝大多数泄漏问题。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 735

【花雕学编程】Arduino FOC 之AS5048A磁编码器模块

您的硬件平台、使用场景和Arduino版本可能影响使用方法的选择。在Arduino平台上实现FOC可以提供平滑的运行和高度的扭矩、速度和位置控制,它通过精确控制电机的电流和电压来实现高效率、高精度和低噪声的操作。5、创新:Arduino可以让你用电子的方式来表达你的创意和想象,你可以用Arduino来制作各种有趣和有用的项目,如机器人、智能家居、艺术装置等。1、高性能电机控制:FOC是一种高级的电机控制算法,可以精准控制PMSM(永磁同步电机)和BLDC(无刷直流)电机,实现平滑的转速和扭矩输出。

雕爷学编程 1496

string类

string类相关函数使用,包括成员函数和非成员重载函数。

xiong2learning的博客 1999

Python爬虫实战:基于Scrapy与Playwright高效爬取人民网新闻数据

本文将详细介绍如何使用Python最新技术栈(Scrapy框架结合Playwright浏览器自动化工具)高效爬取人民网新闻数据。文章包含完整项目搭建、反反爬策略、数据解析存储以及性能优化等核心内容,提供可直接运行的代码示例,适合中高级Python开发者学习现代网络爬虫开发技术。python# 去除特殊空白字符。

2201_76125261的博客 1118

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

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

m0_65245374的博客 1163

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

VB摄像头监控

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

C++string类重要成员函数介绍及自我模拟编写string类

日拱一卒,功不唐捐!一起进步~

qq_68926231的博客 1915

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【C++】string类的相关成员函数以及string的模拟实现

主要介绍了string类中常见的接口,以及相关的成员函数的模拟实现

jhj9201633的博客 870

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【C++】模拟实现string类

使用C++语言模拟实现C++中的string类,包括string类中的成员变量,默认成员函数,成员函数,运算符重载以及迭代器. 是适合C++初学者的入门模拟实现string逐步详解.

C/C++领域,数据结构,linux,MySQl学习者 1733

C++初阶-string类的模拟实现

若写成两个构造函数,一个设置成无参,一个设置成带参,若调用如上的带参构造函数就会报错,将str传给_str,属于权限放大,为了解决这个问题,可以将_str改为const char*类型,但是无法修改_str所指向的内容,调用operator[]函数就会报错。为了避免频繁扩容,使用一个128的字符数组接收,若输入的数据比128小,跳出循环将数组中的数据传给string类s,若输入的数据比128大,则将字符数组整体传给string类s,再正常扩容。

sushang的博客 1330

c++string类的成员函数: max_size、capacity、reserve、resize、find、substr、分离一个网址的协议,域名,?等、clear、insert、empty等的介绍

c++string类的成员函数: max_size、capacity、reserve、resize、find、substr、分离一个网址的协议,域名,?等、clear、insert、empty、erase、replace、swap、c_str、compare、npos、getline等的介绍c++string类的成员函数: max_size、capacity、reserve、resize、find、substr、分离一个网址的协议,域名,?

Farewell_me的博客 925

C++初阶——简单实现string

简单实现std::string类及一些常用的成员函数。迭代器,使用指针简单代替。

博客 | 复习知识点 + 练习 + 最终的领悟 | 全力以赴 445

今天你学C++了吗?——string底层总代码

string底层实现总代码

2401_82924480的博客 724

领悟 C++ string 类模拟实现:触摸代码灵魂的深度感悟

本文通过一系列测试函数对自定义命名空间zzy下的string类进行功能测试,包括构造函数、拷贝构造函数、赋值运算符重载、迭代器相关、容量和大小调整、字符串修改、访问字符串、关系运算符重载以及输入输出运算符重载等功能的测试验证。

阳光开朗大男孩 = ̄ω ̄=的博客 858

C++中string类总结一:基础了解(三个默认成员函数:构造、析构、赋值,并介绍string中迭代器的使用)

C++中string类总结一:基础了解(三个默认成员函数:构造、析构、赋值,并介绍string中迭代器的使用)

SHIE_Ww的博客 801

C++ string 各种操作

转自:http://c.biancheng.net/view/400.html string 类是STL中 basic_string 模板实例化得到的模板类。其定义如下: typedef basic_string <char> string; basic_string 此处可以不必深究。 string 类的成员函数有很多,同一个名字的函数也常会有五六个重载的版本。篇幅所限,不...

flyzer的博客 478

C++string类

C++string类

2402_86037826的博客 702

C++string类常用方法

C++string类常用方法 1、string(const char *s) :将 string 对象初始化为 s 指向的字符串 string str("hello"); 2、string(size_type n,char c) :创建一个包含 n 个元素的 string 对象,其中每个元素都被初始化为字符 c string str(10, 'a'); 3、string(const string &str) :将一个 string 对象初始化为 string 对象 str(复制构造函数) str

Goallegoal的博客 9万+
上一篇: Python字符串操作和string模块代码分析
下一篇: C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法
Scandinavians
博客等级 码龄19年 36粉丝 121原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值