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
文章浏览阅读4k次,点赞5次,收藏31次。卡内基梅隆大学赵越-图神经网络与异常检测_图神经网络 异常检测
文章浏览阅读3.1w次,点赞17次,收藏224次。原创声明http://blog.csdn.net/wonder_coole/article/details/79542241,本博客所有文章均同步发表于www.mx1980.cn/blogSpyGlass工具简介目前包含:SpyGlass Lint, CDC, RDC, DFT ADV, Power. 是业界RTL Signoff 的重要工具. SpyGlass 的一些..._ac conv03
文章浏览阅读5.3k次,点赞7次,收藏31次。众所周知,作为一款优秀的可执行文件压缩工具,UPX受到程序开发者的广泛欢迎。然而一些病毒、木马和其他恶意程序使用了UPX来隐藏自身行为,这使得它拥有不好的名声,导致了杀毒引擎对一些无害的应用程序的误报。有些杀毒引擎甚至将所有加了UPX壳的程序视为病毒,比如ClaimAV、Comodo、F-Prot等等。其实,不仅是UPX,其它的压缩壳,比如ASPack、PECompact也是如此。今天我们要..._upx -d
文章浏览阅读500次,点赞12次,收藏12次。在当今计算机科学和软件工程的领域中,池化技术如线程池、连接池和对象池等已经成为优化资源利用率和提高软件性能的重要工具。然而,在 Python 的协程领域,我们却很少见到类似于 ThreadPoolExecutor 的 CoroutinePoolExecutor。为什么会这样呢?首先,Python Coroutine 的特性使得池化技术在协程中的应用相对较少。与像 Golang 这样支持有栈协程的语言不同,Python Coroutine 是无栈的,无法跨核执行,从而限制了协程池发挥多核优势的可能性。
文章浏览阅读2.2k次。/** * 简单的异或加密算法 * * @author 李奔 * @date 2018年5月21日 * @time 上午10:08:10 */public class DeEnCode { private static final String key0 = "FECOI()*&<MNCXZPKL"; private static final Charset c..._fecoi()*&
文章浏览阅读3k次,点赞3次,收藏13次。char a = 'h';cout << &a;就会出现乱码:h烫烫烫烫篾看了这个贴:https://bbs.csdn.net/topics/310062432改成printf("%p",&a);或者std::cout << (void *)&a;就可以输出地址了。原因:(摘自上面提到的贴子评论)cout <&l..._c++ new char 输出乱码
文章浏览阅读2.4k次,点赞3次,收藏14次。script>name : '烟雨平生',age : 20,sex : "男"_json语法
文章浏览阅读140次。本教程是关于Maya中Python编辑基础核心技术训练视频教程,时长:超过20小时,大小:3.1 GB,MP4高清视频格式,教程使用软件:Maya,附源文件,作者:Geordie Martinez,共8个章节,语言:英语。分享Autodesk Maya是美国Autodesk公司出品的世界顶级的三维动画软件,应用对象是专业的影视广告,角色动画,电影特技等。Maya功能完善,工作灵活,易学易用,制作效..._learn python inside maya
文章浏览阅读111次。要求:1.R4为ISP,其上只能配置IP地址,R4与其他所有直连设备间使用公有IP;解决ospf不规则区域的3种方式都得使用.2.整个osPF环境p地址为172.16.0.0/16进程1oo使用192.168.1.0/243.所有设备均可访问R4的环回r14上有两个环回减少LsA的更新量,加快收敛,保障更新安全4.全网可达解决不规则区域的三种方式1.Tunnel在两台ABR上创建tunnel,然后将其宣告到OSPF协议中;缺点:1、选路不佳 2、周期hello和更新均对中间区域进行影响._interface tunnel1
文章浏览阅读1.8w次,点赞3次,收藏14次。简单来说就是32E和32U是32的升级版本,具有更高的稳定性具体的区别建议官网查看:模组概览 | 乐鑫科技最近有新项目要用到32E,要去某宝购买回来练练手,但是发现有芯片和开发板,开发板更适合新手和软件开发者,但是再一看有搭载32的开发板,3u的开发板,一脸懵,百度了区别但是广告更多,还是决定看官网,对比可以看到32E是32U和32的升级版本,具有更高的稳定性安全性。所以还是选32E的吧,虽然贵的/>_</受不了,上传的图片被压缩了,大家还是官网看把..._esp32wroom32d和32e区别
文章浏览阅读1.5k次。之前一直是使用XSHELL登录服务器,用VIM编写代码,进行调试,但是VSCODE确实太香。偶然发现微软给VSCODE加入了SSH-REMOTE插件,从而在本地使用VSCODE操作服务器上的程序不再梦幻。概述方法其实非常简单:安装openssh生成本地公钥公钥上传服务器配置VSCODE准备工作安装openssh的方法很多,可以用POWERSHEEL命令行安装,也可以下载openssh安装。生成本地公钥的方法跟以前写的方法一样,不再说了。把公钥复制到服务器.ssh文件夹的author_vscode连接linux服务器写代码
文章浏览阅读221次。然后,使用嵌套的循环遍历输入图像的每个像素点(除去边缘像素),并对每个像素点应用Sobel算子进行卷积计算。计算过程中,我们将得到的水平和垂直方向上的边缘强度相加,并将其限制在0-255的范围内。边缘检测对于识别图像中的边缘区域非常有用,并在许多计算机视觉和图像处理应用中得到广泛应用。该算法基于卷积操作,在图像中滑动一个特定的核(矩阵)来计算每个像素点的边缘强度。边缘检测是数字图像处理中的一项重要任务,它用于识别图像中的边缘区域。在本文中,我们将使用C语言来实现基本的边缘检测算法,并计算边缘的特征。_使用c语言实现sobel算子边缘检测