C++ 中,std::string
是用于处理字符串的标准库类。它提供了一系列成员函数和操作符,使得字符串的操作更加方便和灵活。
string与char*的区别:
char*
是一个指针string
本质上是一个类,类的内部封装了char*
,即string
是一个char*
型的容器string
管理char*
所分配的内存,不用担心复制越界和取值越界等构造函数原型 | 解释 | |
---|---|---|
1 | string() | 创建一个空的字符串 |
2 | string(const char* c) | 使用字符串c初始化 |
3 | string(const string& str) | 使用string对象初始化 |
4 | string(int n, char c) | 使用n个字符c初始化 |
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1; //创建空字符串,调用无参构造函数
string s2("hello C++"); //把const char*转换成了string
string s3(s2); //调用拷贝构造函数,使用s2初始化s3
string s4(10, 'c');
cout << "s1 = " << s1 << endl;
cout << "s2 = " << s2 << endl;
cout << "s3 = " << s3 << endl;
cout << "s4 = " << s4 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
s1 =
s2 = hello C++
s3 = hello C++
s4 = cccccccccc
函数原型: += 、append | 解释 | |
---|---|---|
1 | string& operator+=(const char* str) | 重载+=操作符 |
2 | string& operator+=(const char c) | 重载+=操作符 |
3 | string& operator+=(const string& str) | 重载+=操作符 |
4 | string& append(const char *s) | 把字符串s连接到当前字符串结尾 |
5 | string& append(const char *s, int n) | 把字符串s的前n个字符连接到当前字符串结尾 |
6 | string& append(const string &s) | 同3 |
7 | string& append(const string &s, int p, int n) | 将字符串s下标p开始的n个字符连接到当前字符串结尾 |
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "AB";
s1 += "CD"; //第1种拼接方法:+=char*字符串
cout << "s1 = " << s1 << endl;
s1 += 'e'; //第2种拼接方法:+=字符
cout << "s1 = " << s1 << endl;
string s0 = "FG";
s1 += s0; //第3种拼接方法;+=string字符串
cout << "s1 = " << s1 << endl;
string s2 = "AB";
s2.append("CD"); //第4种拼接方法;append("");
cout << "s2 = " << s2 << endl;
s2.append("EFGH",3); //第5种拼接方法;append("",n);
cout << "s2 = " << s2 << endl;
string s3 = "HIJ";
s2.append(s3); //第6种拼接方法;append(string);
cout << "s2 = " << s2 << endl;
string s4 = "KLMNOPQ";
s2.append(s4,0,5); //第7种拼接方法;append(string,p,n);
cout << "s2 = " << s2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
s1 = ABCD
s1 = ABCDe
s1 = ABCDeFG
s2 = ABCD
s2 = ABCDEFG
s2 = ABCDEFGHIJ
s2 = ABCDEFGHIJKLMNO
函数原型:find、rfind、replace | 解释 | |
---|---|---|
1 | int find(const string& s, int p) | 从p开始查找s第一次出现位置 |
2 | int find(const char* s, int p) | 同1 |
3 | int find(const char* s, int p, int n) | 从p位置查找s的前n个字符第一次位置 |
4 | int find(const char c, int p) | 同1 |
5 | int rfind(const string& s, int p) | 从p开始查找s最后一次位置 |
6 | int rfind(const char* s, int p) | 同5 |
7 | int rfind(const char* s, int p, int n) | 从p查找s的前n个字符最后一次位置 |
8 | int rfind(const char c, int p) | 查找字符c最后一次出现位置 |
9 | string& replace(int p, int n, const string& s) | 替换p开始n个字符为字符串s |
10 | string& replace(int p, int n,const char* s) | 同9 |
注意:
find
查找是从左向后,rfind
从右向左find
与rfind
找到指定字符串时,返回查找的第一个字符的位置下标;未找到则返回-1replace
在替换时,要指定起始位置p,替换字符数量n,替换后的字符串s示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
//查找
string s1 = "ABCDEFGHIGK";
int p = s1.find("EF");
if (p == -1)
{
cout << "not find!" << endl;
}
else
{
cout << "find p = " << p << endl;
}
p = s1.rfind("EF");
if (p == -1)
{
cout << "not find!" << endl;
}
else
{
cout << "rfind p = " << p << endl;
}
}
void test02()
{
//替换
string s1 = "ABCDEF";
s1.replace(1, 3, "0000");
cout << "s1 = " << s1 << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
//result
find p = 4
rfind p = 4
s1 = A0000EF
函数原型:compare | 解释 | |
---|---|---|
1 | int compare(const string &s) | 与字符串s比较 |
2 | int compare(const char *s) | 同1 |
注意:
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "aaaaa";
string s2 = "AAAAA";
int ret = s1.compare(s2);
if (ret == 0)
{
cout << "s1 等于 s2" << endl;
}
else if (ret > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
//result
s1 大于 s2
函数原型:[]、at、insert、erase | 解释 | |
---|---|---|
1 | char& operator[](int n) | 重载[]操作符 |
2 | char& at(int n) | 通过.at方法获取字符 |
3 | string& insert(int p, const char* s) | 在下标p处插入字符串 |
4 | string& insert(int p, const string& str) | 同3 |
5 | string& insert(int p, int n, char c) | 在指定位置插入n个字符c |
6 | string& erase(int p, int n) | 删除从P开始的n个字符 |
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s = "ABCDEFG";
cout << "s[1] = " << s[1] << endl;
//修改s的第二个字符
s.at(1) = 'X';
cout << "s.at(1)" << s.at(1) << endl;
//插入字符串
s.insert(1, "555");
cout << "s.insert = " << s << endl;
//删除字符串
s.erase(1, 3);
cout << "s.erase = " << s << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
s[1] = B
s.at(1)X
s.insert = A555XCDEFG
s.erase = AXCDEFG
函数原型:substr | 解释 | |
---|---|---|
1 | string substr(int p = 0, int n) | 返回由p开始的n个字符组成的字符串 |
示例:
#include <iostream>
#include <string>
using namespace std;
void test01()
{
string s1 = "ABCDEFG";
//截取子字符串
string s2 = s1.substr(1,4);
cout << "s2 = " << s2 << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
//result
s2 = BCDE
文章浏览阅读3.5k次,点赞2次,收藏13次。为了从FTP服务器下载文件,需要要实现一个简单的FTP客户端。FTP(文件传输协议) 是 TCP/IP 协议组中的应用层协议。FTP协议使用字符串格式命令字,每条命令都是一行字符串,以“\r\n”结尾。客户端发送格式是:命令+空格+参数+"\r\n"的格式服务器返回格式是以:状态码+空格+提示字符串+"\r\n"的格式,代码只要解析状态码就可以了。读写文件需要登陆服务器,特殊用..._ftp 登录返回230
文章浏览阅读648次。前提:systemctl stop firewalld 关闭防火墙关闭selinux查看getenforce临时关闭setenforce 0永久关闭sed-i'/SELINUX/s/enforcing/disabled/'/etc/selinux/configselinux的三种模式enforcing:强制模式,SELinux 运作中,且已经正确的开始限制..._centos7 安装rabbitmq3.6.5
文章浏览阅读5.8k次。满意答案s55f2avsx2017.09.05采纳率:46%等级:12已帮助:5646人新版Android Studio/IntelliJ IDEA可以直接导入eclipse项目,不再推荐使用eclipse导出gradle的方式2启动Android Studio/IntelliJ IDEA,选择 import project3选择eclipse 项目4选择 create project f..._android studio 项目导入idea 看不懂安卓项目
文章浏览阅读860次,点赞2次,收藏6次。AI大模型技术已经在自然语言处理、计算机视觉、多模态交互等领域取得了显著的进展和成果,同时也引发了一系列新的挑战和问题,如数据质量、计算效率、知识可解释性、安全可靠性等。城市运维涉及到多个方面,如交通管理、环境监测、公共安全、社会治理等,它们需要处理和分析大量的多模态数据,如图像、视频、语音、文本等,并根据不同的场景和需求,提供合适的决策和响应。知识搜索有多种形式,如语义搜索、对话搜索、图像搜索、视频搜索等,它们可以根据用户的输入和意图,从海量的数据源中检索出最相关的信息,并以友好的方式呈现给用户。_ai大模型应用开发
文章浏览阅读8.2k次,点赞12次,收藏121次。为什么要测量阻抗呢?阻抗能代表什么?阻抗测量的注意事项... ...很多人可能会带着一系列的问题来阅读本文。不管是数字电路工程师还是射频工程师,都在关注各类器件的阻抗,本文非常值得一读。全文13000多字,认真读完大概需要2小时。一、阻抗测试基本概念阻抗定义:阻抗是元器件或电路对周期的交流信号的总的反作用。AC 交流测试信号 (幅度和频率)。包括实部和虚部。图1 阻抗的定义阻抗是评测电路、元件以及制作元件材料的重要参数。那么什么是阻抗呢?让我们先来看一下阻抗的定义。首先阻抗是一个矢量。通常,阻抗是_阻抗实部和虚部
文章浏览阅读955次。前面章节分享试用了pyzero,pygame但随着想增加更丰富的游戏内容,好多还要进行自己编写类,从今天开始解绍一个新的python游戏库arcade模块。通过此次的《连连看》游戏实现,让我对swing的相关知识有了进一步的了解,对java这门语言也有了比以前更深刻的认识。java的一些基本语法,比如数据类型、运算符、程序流程控制和数组等,理解更加透彻。java最核心的核心就是面向对象思想,对于这一个概念,终于悟到了一些。_arcade语言 like
文章浏览阅读1.1k次。源码简介与安装说明:2021增强版短视频去水印源码 去水印微信小程序源码网站 去水印软件源码安装环境(需要材料):备案域名–服务器安装宝塔-安装 Nginx 或者 Apachephp5.6 以上-安装 sg11 插件小程序已自带解析接口,支持全网主流短视频平台,搭建好了就能用注:接口是公益的,那么多人用解析慢是肯定的,前段和后端源码已经打包,上传服务器之后在配置文件修改数据库密码。然后输入自己的域名,进入后台,创建小程序,输入自己的小程序配置即可安装说明:上传源码,修改data/_去水印机要增强版
文章浏览阅读557次。1. 触发器是FPGA存储数据的基本单元2. 触发器作为时序逻辑的基本元件,官方提供了丰富的配置方式,以适应各种可能的应用场景。_fdre #(.init(1'b0) // initial value of register (1'b0 or 1'b1) ) fdce_osc (
文章浏览阅读560次。本该是不同编译器结果不同,但是尝试了g++ msvc都是先计算c,再计算b,最后得到a+b+c是经过赋值以后的b和c参与计算而不是6。由上表可知,将q复制到p数组可以表示为:*p++=*q++,*优先级高,先取到对应q数组的值,然后两个++都是在后面,该行运算完后执行++。在电脑端编译完后会分为text data bss三种,其中text为可执行程序,data为初始化过的ro+rw变量,bss为未初始化或初始化为0变量。_嵌入式面试笔试c语言知识点
文章浏览阅读2.3k次。57 Things I've Learned Founding 3 Tech CompaniesJason Goldberg, Betashop | Oct. 29, 2010, 1:29 PMI’ve been founding andhelping run techn_mature
文章浏览阅读1.9k次。问题:先讲下需求,有若干个文本文件(txt或者csv文件等),每行代表一条数据,现在希望能合并成 1 个文本文件,且需要去除重复行。分析:一向奉行简单原则,如无必要,绝不复杂。如果数据量不大,那么如下两条命令就可以搞定合并:cat a.txt >> new.txtcat b.txt >> new.txt……去重:cat new...._python 超大文本合并
文章浏览阅读489次。这个过渡页是第一次打开小程序展示的,点击某个小程序前把手机的开发者->network link conditioner->enable & very bad network 就会在停在此页。比如《支付宝运动》这个小程序先看这个类的.h可以看到它继承于DTViewController点击左上角返回的方法- (void)back;#import "DTViewController.h"#import "APBaseLoadingV..._类似支付宝页面过度加载页