STL常用容器——String容器的使用

2023-05-16

文章目录

  • STL常用容器——String容器
    • 1、string构造器
    • 2、string的赋值操作
    • 3、拼接字符串
    • 4、字符串查找和替换
    • 5、字符串比较
    • 6、字符串存取
    • 7、字符串插入与删除
    • 8、截取字符串

STL常用容器——String容器

string类封装了很多成员方法,例如查找find,拷贝copy,删除delete,替换replace,插入insert。

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部负责

1、string构造器

string();创建一个空字符串

string (const char *s);用字符串s 进行初始化

string (const string& str);使用一个string对象,初始化另一个string对象

string(int n,char c);使用n个字符初始化

#include<iostream>
using namespace std;
#include<string>

void test()
{
	string s;
	cout << s << endl;

	string s1("明天,你好");
	cout << s1 << endl;

	string s2(s1);
	cout << s2 << endl;

	string s3(10, 'w');
	cout << s3 << endl;

}
int main() {

	test();
	system("pause");
	return 0;
}

2、string的赋值操作

sting& operator=(const chars); char字符串赋值给当前字符串

sting& operator=(const string &s);把字符串s赋值给当前字符串

sting& operator=(const char c); 字符c赋值给当前字符串

sting& assign(const char*s); 把字符串s赋值给当前字符串

sting& assign(const char*s,int n); 把字符串s的前n个字符赋值给当前字符串

sting& assign(const string &s); 把字符串s赋值给当前字符串

sting& assign(int n,char c); 把n个字符c赋值给当前字符串

#include<iostream>
using namespace std;
#include<string>

void test() {
	const char* s = "你好呀";
	string s1 = s;
	cout << s1 << endl;

	string s2 = s1;
	cout << s2 << endl;

	string s3;
	s3 = 'a';
	cout << s3 << endl;

	string s4;
	s4.assign("明天见");
	cout << s4 << endl;

    //截取,[0,3)
	string s5;
	s5.assign("abcdefj", 3);
	cout << s5 << endl;

	string s6;
	s6.assign(s1);
	cout << s6 << endl;

	string s7;
	s7.assign(6, 'a');
	cout << s7 << endl;
}
int main() {
	test();
	system("pause");
	return 0;
}

3、拼接字符串

运算符+=重载:

  • sting& operator+=(const char* str);

  • sting& operator+=(const string &s);

  • sting& operator+=(const char c);

append方法:

  • sting& appendconst char*s); 把字符串s连接到当前字符串结尾

  • sting& append(const char*s,int n); 把字符串s的前n个字符连接到当前字符串结尾

  • sting& append(const string &s); 相当于operator+=(const string &s);

  • sting& append(const string &s,int pos,int n);字符串s中从pos开始的n个字符连接到当前字符串结尾

#include<iostream>
using namespace std;
#include<string>

void test01() {
	string s = "确实";
	string s1 = "我爱游戏";
	cout << s1 << endl;
	s1 += "?你在打游戏吗";
	cout << s1 << endl;
	s1 += '!';
	cout << s1 << endl;
	//中文占2个字符大小
	string s2 = "今天天气真不错";
	cout << s2 << endl;
	s2.append(",hhh");
	cout << s2 << endl;
	s2.append(s);
	cout << s2 << endl;
	s2.append("快别打了,这都啥呀?", 4);
	cout << s2 << endl;
	s2.append(s1, 4, 5);
	cout << s2 << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

4、字符串查找和替换

查找

int find(const string& str,int pos=0) const; 返回str第一次出现的位置下标,从pos开始查找,如果没有查找到则返回-1

int find(const char* s,int pos=0) const; 查找s第一次出现的位置下标,从pos开始查找

int find(const char* s,int pos=0,int n) const; 从pos开始查找s的前n个字符第一次出现的位置下标

int find(const char c) const; 查找字符c第一次出现的位置下标

int rfind(const string& str,int pos=npos) const; 查找str最后一次出现的位置下标,从pos开始查找

int rfind(const char* s,int pos=npos) const; 查找s最后一次出现的位置下标,从pos开始查找

int rfind(const char* s,int pos=npos,int n) const; 从pos开始查找s的前n个字符最后一次出现的位置下标

int rfind(const char c) const; 查找字符c最后一次出现的位置下标

替换

string &replace(int pos,int n,const string &str);替换从pos开始的n个字符为字符串str

string &replace(int pos,int n,const char* s);替换从pos开始的n个字符为字符串s

#include<iostream>
using namespace std;
#include<string>
//字符串的查找
void test01() {
	string s1 = "abcdefghijkeflmn";
	int pos = s1.find("ef");
	cout << pos << endl; //4 返回的是查找目标字符串的首个字符对应第一次的位置下标
	int res = s1.find("wn");
	cout << res << endl;//-1  没有查找到则返回-1

	int pos1 = s1.rfind("ef");
	cout << pos1 << endl;//11 rfind查找最后一次出现的位置

}
//字符串的替换
void test02() {
	string s1 = "abcdefghijkeflmn";
	s1.replace(2, 4, "开始了");//ab开始了ghijkeflmn  替换掉[2,4]范围的字符串
	cout << s1 << endl;
}
int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

5、字符串比较

字符串比较最大用途是比较是否相等,对于比较大小没有实在意义

字符串比较规则:

  • 按字符的ASCII码进行对比
  • “=” 返回 0
  • “>” 返回 1
  • “<” 返回 -1
#include<iostream>
using namespace std;
#include<string>

void test01() {
	string s1 = "cccc";
	string s2 = "cccc";
	string s3 = "dddd";
	string s4 = "aaaa";

	cout << s1.compare(s2) << endl;//0
	cout << s1.compare(s3) << endl;//-1
	cout << s1.compare(s4) << endl;//1

}
int main() {
	test01();
	system("pause");
	return 0;
}

6、字符串存取

char& operator[](int n) 通过[]取字符串

char& at(int n) 通过at方式获取字符串

#include<iostream>
using namespace std;
#include<string>
//字符串存取
void test01() {
	//读取中文时输出的是双倍的“?”
	string s = "hello,世界";
	//通过[]获取
	for (int i = 0; i < s.size(); i++) {
		cout << s[i] << " ";
	}
	cout << endl; // h e l l o , ????
	//通过at获取
	for (int i = 0; i < s.size(); i++) {
		cout << s.at(i) << " ";
	}
	cout << endl; // h e l l o , ????

	//修改单个字符
	s[0] = 'c';
	cout << s << endl; //cello,世界
	s.at(1) = 'c';
	cout << s << endl;//ccllo,世界

}
int main() {
	test01();
	system("pause");
	return 0;
}

7、字符串插入与删除

string& insert(int pos,const char *s); 插入字符串

string& insert(int pos,const string& str); 插入字符串

string& insert(int pos,int n,char c);在指定位置pos插入n个字符c

string& erase(int pos,int n =npos); 删除从npos开始的n个字符,范围为[0,5)

#include<iostream>
using namespace std;
#include<string>
//string的插入与删除
//1. string & insert(int pos, const char* s);   //插入字符串
//2. string & insert(int pos, const string & str); //插入字符串
//3. string & insert(int pos, int n, char c);//在指定位置pos插入n个字符c
//4. string & erase(int pos, int n = npos); //删除从pos开始的n个字符
void test01() {
	//string的插入
	string s = "hello,世界";
	const char* s1 = "521";
	s.insert(4, s1);
	cout << s << endl;//hell521o,世界 插入到第四个位置
	s.insert(1, "新增字符串");
	cout << s << endl;//h新增字符串ell512o,你好  插入到第一个位置
	s.insert(0, 6, 'd');
	cout << s << endl;//ddddddh新增字符串ell512o,你好   在s的指定位置插入n个字符
	//string 的删除
	s.erase(0, 5);
	cout << s << endl;//dh我是新增的字符串ell512o,你好  删除[0,5)范围的字符
}
int main() {
	test01();
	system("pause");
	return 0;
}

8、截取字符串

str.substr(int pos=0,int n=npos) const; 截取str中由pos开始的n个字符组成的字符串

#include<iostream>
using namespace std;
#include<string>
//string获取子串

void test01() {
	string s = "hello,世界";
	string subS = s.substr(2, 7);
	cout << subS << endl;//llo,世  若字符串的截取个数恰好读到中文且只剩一个字符,那么中文不显示
}
void test02() {
	string s1 = "wujingou@qq.com";
	string sub = s1.substr(0, s1.find('@'));
	cout << "截取到的用户名为:" << sub << endl;//截取到的用户名为:wujingou
}
int main() {
	test01();

	test02();
	system("pause");
	return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

STL常用容器——String容器的使用 的相关文章

  • maven

    http blog csdn net zjf280441589 article details 53044308 http www infoq com maven Porject groupId 43 artifactId 43 versi
  • Linux第二课:Ubuntu 操作入门(内含:1Ubuntu 下打开终端+2 Linux 文件属性+3 设置屏幕+4 系统关机与重启+5.文件浏览器)

    Ubuntu 操作入门 2 2 1Ubuntu 下打开终端 方法1 点击 Ubuntu 桌面左上角图标进入搜索框 xff0c 输入 term 可以弹出终端 Terminal 程序 方法2 xff1a 桌面或者在文件浏览器的任何目录下右键鼠标
  • 堆中存什么?栈中存什么?

    堆中存的是对象 栈中存的是基本数据类型 和堆中对象的引用 一个对象的大小是不可估计的 xff0c 或者说是可以动态变化的 xff0c 但是在栈中 xff0c 一个对象只对应了一个4btye的引用 xff08 堆栈分离的好处 xff1a xf
  • 计算一个数的N次方

    计算一个数的N次方时 xff0c 我们先设定两个参数n和k xff0c n表示你要输入的数 xff0c k表示这个数的次方 这个时候我们必须对次方数k作出分类 xff1a k 61 0 return 1 其他 xff1a return n
  • 用结构体编写电话通讯录

    用结构体数组编写电话通讯录 xff0c 必须得知道结构体的形式 xff0c 那先把结构体定义回顾一下 xff1a 一般形式为 xff1a xff08 1 xff09 struct 结构体名称 成员表列 数组名 数组长度 如 xff1a st
  • linux(centos)下安装git并上传代码些许步骤(亲自验证过的步骤)

    以前听说了好多次github xff0c 但直到最近才第一次学习使用github来托管自己在linux下的代码 xff01 说实话 xff0c 我自己在使用的时候从网上查了好多教程 xff0c 但总觉得难以掌握 xff08 步骤过于繁琐 x
  • shell具体执行过程及自主实现shell解释器

    在编写shell 解释器之前 xff0c 先来分析几个知识点 xff1a xff08 1 xff09 shell 执行命令时步骤 xff1a xff08 如下图 xff09 xff08 2 xff09 shell 执行脚本时的步骤 xff1
  • Linux下的桥接模式和Nat模式的区别

    先来看一下linux在的桥接模式和Nat模式的差别 xff1a 桥接模式 xff1a Nat模式 xff1a 真正的接触这个问题是因为同学要给我远程传输文件 xff0c 这个时候就调节至桥接模式下 xff0c 进行ping 尽管我们用的是同
  • C知识点整合

    C语言总结 一 语法 1 常见的数据内置类型所占字节 xff08 64 位下 xff09 xff1a char 1 int 4 float 4 long 4 double 8 Longlong 8 2 变量 xff1a xff08 1 xf
  • 判断一棵二叉树是否为完全二叉树

    1 完全二叉树的特点 xff08 来自专业定义 xff09 看到上面完全二叉树的特点 xff0c 我可以将其特点按照自己的 理解归纳为以下几点 xff1a xff08 1 xff1a 若二叉树最下面一层有节点出现 xff0c 那么这个节点一
  • 深入理解JNI技术

    一 JNI是什么 xff1f JNI是Java Native Interface的缩写 xff0c 译为Java本地调用 JNI是一种技术 二 JNI技术的用途 xff1f Java程序中的函数调用Native程序中的函数 Native一般
  • HTTP基本认证(Basic Authentication)

    在浏览网页时候 xff0c 浏览器会弹出一个登录验证的对话框 xff0c 如下图 xff0c 这就是使用HTTP基本认证 1 客户端发送http request 给服务器 服务器验证该用户是否已经登录验证过了 xff0c 如果没有的话 xf
  • 将字符串以单词为单位逆序"I am a Student" 解法

    网上有个题目 xff0c 将字符串以单词为单位逆序 例如 34 I am a Student 34 要变成 34 Student a am I 34 解法大致为 xff1a 先将字符串整体逆序第一个字符和最后一个交换 xff0c 第二个与倒
  • 堆排序查找前N个最大数和二分查找算法

    先了解堆排序概念 堆排序利用了大根堆 xff08 或小根堆 xff09 堆顶记录的关键字最大 xff08 或最小 xff09 这一特征 xff0c 使得在当前无序区中选取最大 xff08 或最小 xff09 关键字的记录变得简单 xff08
  • 构建hash表和两种处理冲突方法

    hash表定义 hashing定义了一种将字符组成的字符串转换为固定长度 xff08 一般是更短长度 xff09 的数值或索引值的方法 xff0c 称为散列法 xff0c 也叫哈希法 由于通过更短的哈希值比用原始值进行数据库搜索更快 xff
  • 用hash_map统计出现次数最多的前N个URL

    海量数据统计频率最高词汇的常规办法之一是先通过一个hash函数处理数据然后取模N xff0c 拆分为N个小文件 xff0c 对每一个小文件进行词频统计和排序处理 xff0c 然后归并N个小文件取频率最大的M个数 下面程序是利用hash ma
  • C++与java语法的异同整理

    文章目录 Java与C 43 43 与基础语法异同java的认知 amp java与C 43 43 的异同C 43 43 中的虚函数和JAVA中的抽象方法区别C 43 43 虚函数与JAVA中抽象函数比较关于接口与抽象类 xff1a Jav
  • 微信公众平台-股票行情查询

    微信公众平台 股票行情查询 php实现的获取上证 xff0c 深证 A B股实时行情的接口 xff0c 只实现了文本消息回复 xff0c K线图可以在图文消息中加上接口url地址就可以显示 xff0c 具体的接口地址网上可以找 xff0c
  • PELCO-D与PELCO-P协议介绍

    一般控制协议都由硬件或软件商编制在程序里面 xff0c 我们只需要通过相关的控制设备来进行操作 但是作为一个从事监控行业的技术人员 xff0c 往往会遇到除了电脑和协议转换器以外根本没有任何控制设备的情况 xff0c 此时 xff0c 协议
  • ROS 问题(topic types do not match、topic datatype/md5sum not match、msg xxx have changed. rerun cmake)

    1 topic types 不匹配 使用 roslaunch 命令 roslaunch carla ros bridge carla ros bridge with example ego vehicle launch 启动官方 demo

随机推荐

  • Ubuntu中查看安装的Python版本以及不同版本之间切换

    查看系统中已安装的所有Python版本 使用 ls 命令来查看你的系统中都有那些 Python 的二进制文件可供使用 xiyou 64 span class token property xiyou virtual machine span
  • Ubuntu Python 多版本安装

    概述 由于 Python 3 有几次较为跳跃的更新 xff0c 导致大量使用 Python 3 作为开发工具的软件会对 Python 3 的版本进行严格限制 xff0c 如限制使用 Python 3 8 Python 3 9 版本 这要求开
  • 怒爬某 Hub 资源就为撸了一个鉴黄平台

    来源 xff1a 码匠笔记公号 黄色已经是我们所不容然而却防不胜防的 xff0c 尤其是对于做内容的工具和平台 xff0c 所以花了30分钟搭建了一个鉴黄平台 xff0c 分享给大家 数据准备 找了 N 多资源都不能解决问题 xff0c 于
  • ROSERROR : datatype/md5sum

    出错原因是 xff1a 自定义消息 xff0c 发送话题的消息类型和接受话题的消息类型不一样 但是我的代码真的是一样的 所以 xff0c 解决办法是 xff1a 清空工作空间的build devel文件夹 xff0c 重新编译运行 成功 x
  • 学习使用 ArUco 标记

    在本文中 xff0c 我们将研究使用 Python 和 OpenCV 检测和估计 ArUco 标记的方向 首先 xff0c 我们将从生成 ArUco 标记开始 你可以使用特定网站一个一个地创建单个标签 xff0c 也可以使用我的程序生成整个
  • TCP协议中的序列号

    TCP 协议工作在OSI的传输层 xff0c 是一种可靠的面向连接的数据流协议 xff0c TCP之所以可靠 xff0c 是因为它保证了传送数据包的顺序 顺序是用一个序列号来保证的 响应包内也包括一个序列号 xff0c 表示接收方准备好这个
  • c++开发过程中遇到的问题及解决方案

    xfeff xfeff 问题一 xff1a 1 gt JForm obj error LNK2019 无法解析的外部符号 34 public virtual thiscall JFC JForm JForm void 34 1JForm 6
  • Digest Auth 摘要认证

    Digest Auth 摘要认证 1 非常规方式 转载 xff1a https blog csdn net qq 25391785 article details 86595529 public static void postMethod
  • 嵌入式C语言基础知识--位操作

    目录 大小端模式 xff1a 字节高低位 xff1a LSB和MSB xff1a 高位先行msb 低位先行lsb xff1a 串口传输是低位先行 IIC传输是高位先行 字节序 比特序 大端模式 小端模式 高字节序 低字节序 MSB LSB
  • qt 绘制 流程图 案例 收集

    参考 C 43 43 中的例子 xff1a Qt Qt5 11 1 Examples Qt 5 11 1 widgets graphicsview diagramscene The Diagram Scene example is an a
  • 获得GPS数据的两种方法 1.读串口

    获得GPS数据一般可通过两种方法 xff0c 读串口及调用gpsapi函数 串口作为硬件设备 xff0c 不能同时被两个程序占用 xff0c gpsapi函数几个应用程序可同时共享端口 1 xff0e 读串口 先找出 gps 使用的串口号
  • 机器人履带底盘的悬挂和传动

    我爸是坦克 xff01 一个履带机器人 一 前言 曾经调研过机器人履带底盘的设计和构造 xff0c 整理一下备忘 本文主要关注自己比较难理解的履带底盘悬挂机构和摇臂式履带底盘传动机构 其中履带底盘的悬挂涉及到克里斯蒂悬挂 xff08 Chr
  • Python爬虫——Scrapy 的基本使用

    文章目录 Python爬虫 Scrapy 的基本使用1 创建 Scrapy 爬虫项目2 Scrapy 创建爬虫文件3 Scrapy 运行爬虫文件 Python爬虫 Scrapy 的基本使用 Scrapy 框架中创建项目 查看配置信息 xff
  • Python爬虫——Scrapy框架使用实例及执行过程

    文章目录 Python爬虫 Scrapy框架使用实例及执行过程1 Scrapy框架使用实例2 Scrapy框架执行过程 Python爬虫 Scrapy框架使用实例及执行过程 1 Scrapy框架使用实例 1 创建scrapy项目 scrap
  • C++基础入门

    文章目录 C 43 43 基础入门1 变量和常量2 关键字3 数据类型4 流程控制5 数组6 函数7 指针8 结构体 C 43 43 基础入门 1 变量和常量 变量和常量 变量 xff1a 数据类型 变量名 61 值 常量 xff1a 宏常
  • 解决CLion的 CMake executable not found:XXX

    解决CLion的 CMake executable not found XXX 问题 CLion不能编译DeepStream项目 xff0c 也不能 build 和 debug了 xff0c 还出现以下提示 xff1a 原因 原因是 cli
  • C++核心编程

    C 43 43 核心编程 1 c 43 43 内存模型 c 43 43 程序执行时将内存大致分为4个区域 代码区 xff1a 存放CPU执行的二进制代码指令 xff0c 由操作系统进行管理全局区 xff1a 存放全局变量和静态变量以及全局常
  • STL技术——STL概述和入门

    文章目录 STL技术 STL概述和入门1 STL简介2 入门案例2 1 vecto存放内置数据类型2 2 vecto存放自定义数据类型2 3 容器嵌套 STL技术 STL概述和入门 1 STL简介 STL介绍 STL xff08 stand
  • 使用keil软件添加.C文件和.H文件到工程

    使用keil软件添加 C文件和 H文件到工程 1 第一步 在所建工程的文件夹下的HARDWARE子文件夹下创建一个所要添加文件名称 xff0c 例如要添加led c和led h文件 xff0c 可以先在HARDWARE文件目录下创建一个命名
  • STL常用容器——String容器的使用

    文章目录 STL常用容器 String容器1 string构造器2 string的赋值操作3 拼接字符串4 字符串查找和替换5 字符串比较6 字符串存取7 字符串插入与删除8 截取字符串 STL常用容器 String容器 string类封装