iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++string容器基本概念详解
  • 416
分享到

C++string容器基本概念详解

2024-04-02 19:04:59 416人浏览 安东尼
摘要

string基本概念 本质: string是c++风格的字符串,而string本质上是一个类 string和char*区别: char*是一个指针 string是一

string基本概念

本质:

string是c++风格的字符串,而string本质上是一个类

string和char*区别:

  • char*是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器

特点:
string类内部封装了很多成员方法
例如:查找find,拷贝copy,删除delete,替换replace,插入insert
string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责。

string构造函数


#include <iOStream>
using namespace std;
#include<string>
//string 的构造函数
void test01()
{
	//默认构造
	string s1;
	 //string(const char* s);使用字符串s初始化
	const char* str = "hello,world";
	string s2(str);
	cout << "s2= " << s2 << endl;
	//string(const string& str)使用一个string对象初始化另一个string对象
	string s3(s2);
	cout << "s3= " << s3 << endl;
	//string(int n,char c);使用n个字符c初始化
	string s4(10, 'a');
	cout << s4 << endl;
}
int main()
{
	test01();
}

string赋值操作

功能描述:

给string字符串进行赋值


#include <iostream>
using namespace std;
#include<string>
//string的赋值操作
void test01()
{
	//string& operator=(const char* s)//char*类型字符串 赋值给当前的字符串
	string str1;
	str1 = "hello,world";
	cout << "str1=" <<str1<< endl;
	//string& operator=(const string &s);//把字符串s赋给当前的字符串
	string str2;
	str2 = str1;
	cout << "str2= " << str2 << endl;
	//string& operator=(char c);//字符赋值给当前的字符串
	string str3;
	str3 = 'a';
	cout << "str3= " << str3 << endl;
	//string& assign(const char *s);//把字符串赋给当前的字符串
	string str4;
	str4.assign("hello C++");
	cout << "str4= " << str4 << endl;
	//string& assign(const char *s,int n);//把字符串s的前n个字符赋给当前的字符串
	string str5;
	str5.assign("hello C++", 5);
	cout << "str5= " << str5 << endl;
	//string& assign(const string &s);//把字符串s赋给当前字符串
	string str6;
	str6.assign(str5);
	cout << "str6= " << str6 << endl;

	string str7;
	str7.assign(10, 'w');
	cout << "str7= " << str7 << endl;

}
int main()
{
	test01();
}

string字符串拼接

功能描述:

实现在字符串末尾拼接字符串


#include <iostream>
using namespace std;
#include<string>
//string字符串拼接
void test01()
{
	//第一种方法:重载+=操作符
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1= " << str1 << endl;
	str1 += ':';
	cout << "str1= " << str1 << endl;
	string str2 = "LOL";
	str1 += str2;
	cout << "str1= " << str1 << endl;
	//把字符串s连接到当前字符串结尾
	string str3 = "I";
	str3.append("love");
	cout << "str3= " << str3 << endl;
	//把字符串s的前n个字符连接到当前字符字符串结尾
	str3.append("game abcde", 4);
	cout << "str3= " << str3 << endl;
	//用append追加一个字符串
	str3.append(str2);
	cout << "str3= " << str3 << endl;
	//字符串s从pos开始的n个字符连接到字符串结尾
	str3.append(str2, 0, 3);//参数2:从哪个位置开始截取,参数3 截取字符个数
	cout << "str3= " << str3 << endl;
}
int main()
{
	test01();
}

string查找和替换操作

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定位置替换字符串

#include <iostream>
using namespace std;
#include<string>
//字符串的查找和替换
//1.查找
void test01()
{
	string str1 = "abcdefg";
	int pos=str1.find("de");
	if (pos == -1)
	{
		cout << "未找到字符串" << endl;
	}
	else
	{
		cout << "找到字符串pos: " << pos << endl;
	}

	//rfind
	pos=str1.rfind("de");
	cout << "pos= " << pos << endl;
	//rfind和find的区别
	//rfind从右往左查找,find从左往右查找
}
//2.替换
void test02()
{
	string str1 = "abcdefg";
	//replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串
	//从1号位置起3个字符替换为"1111"
	str1.replace(1, 3, "1111");
	cout << "str1= " << str1 << endl;
}
int main()
{
	test01();
	test02();
}

string字符串比较

功能描述:

  • 字符串之间的比较
  • 字符串比较是按字符的ASCII码进行比较
  • =返回 0

返回 1

< 返回 -1


#include <iostream>
using namespace std;
#include<string>
//字符串比较
void test01()
{
	string str1 = "A";
	string str2 = "a";
	if (str1.compare(str2) == 0)
	{
		cout << "str1等于str2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
		cout << "str1大于str2" << endl;
	}
	else if (str1.compare(str2) < 0)
	{
		cout << "str1小于str2" << endl;
	}

}
int main()
{
	test01();
}

string字符存取

string中单个字符存取方式有两种:

  • char& operator[](int n);//通过[]方式取字符
  • char& at(int n);//通过at方法获取字符

#include <iostream>
using namespace std;
#include<string>
//string 字符存取
void test01()
{
	string str = "hello";
	cout << "str= " <<str << endl;
	//通过[]访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i] << endl;
	}
	//通过at方式访问单个字符
	for (int i = 0; i < str.size(); i++)
	{
		cout << str.at(i) << endl;
	}
	//修改单个字符
	str[0] = 'x';
	str.at(1) = 'x';
	cout << "str= " << str << endl;
}
int main()
{
	test01();
}

string插入和删除

功能描述:

对string字符串进行插入和删除字符操作


#include <iostream>
using namespace std;
#include<string>
//字符串的插入和删除
void test01()
{
	string str = "hello";
	//插入
	str.insert(1, "111");
	cout << "str= " << str << endl;
	//删除
	str.erase(1, 3);
	cout << "str= " << str << endl;
}
int main()
{
	test01();
}

string子串

功能描述:

从字符串中获取想要的子串‘


#include <iostream>
using namespace std;
#include<string>
//string求子串
void test01()
{
	string str = "abcdef";
	string subStr = str.substr(1, 3);
	cout << "subStr= " << subStr << endl;
}
int main()
{
	test01();
}

到此这篇关于C++string容器基本概念详解的文章就介绍到这了,更多相关C++string容器内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++string容器基本概念详解

本文链接: https://www.lsjlt.com/news/129578.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++string容器基本概念详解
    string基本概念 本质: string是C++风格的字符串,而string本质上是一个类 string和char*区别: char*是一个指针 string是一...
    99+
    2022-11-12
  • C++泛型编程基本概念详解
    目录1.什么是泛型编程?2.函数模板(1)函数模板概念(2)函数模板格式(3)函数模板的原理(4)函数模板的实例化(5)模板参数的匹配原则3.类模板(1)类模板的定义格式(2)类模板...
    99+
    2022-11-12
  • JAVA基本概念详解
    目录一、java发展史二、Java跨平台二、JRE和JDK三、下载JDK四、常用的DOS命令五、配置Path环境变量六、第一个程序六、bug(甲壳虫)七、Notepad八、注释九、关...
    99+
    2022-11-12
  • C/C++程序设计的基本概念详解
    目录概述main()函数内部名称变量和它的初始化编译时和运行时总结概述 学C语言有很长一段时间了,想做做笔记,把C和C++相关的比较容易忽视的地方记下来,也希望可以给需要的同学一些帮...
    99+
    2022-11-12
  • C/C++中线程基本概念与创建详解
    目录一、线程基本概念二、为什么使用多线程1.避免阻塞2.避免CPU空转3.提升效率三、创建线程函数1.CreateThread2._beginthreadex四、简单多线程示例使用C...
    99+
    2022-11-13
  • Node.js的4个基本概念详解
    本篇内容主要讲解“Node.js的4个基本概念详解”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Node.js的4个基本概念详解”吧!1.非阻塞或异步I/O由于...
    99+
    2022-10-19
  • VB.NET多维数组基本概念详解
    VB.NET中的多维数组是一种特殊的数据结构,它可以存储多个值,并且可以按照多个维度进行索引和访问。多维数组的基本概念包括以下几点:...
    99+
    2023-09-22
    VB.NET
  • K8s-helm简介及基本概念详解
    目录Helm简介一、什么是 Helm(官网:https://helm.sh/)二、Helm中的基本概念Chart三、从Helm2到Helm3的变化四、Helm版本支持策略Helm简介...
    99+
    2022-11-13
  • 基于线程、并发的基本概念(详解)
    什么是线程?提到“线程”总免不了要和“进程”做比较,而我认为在Java并发编程中混淆的不是“线程”和“进程”的区别,而是“任务(Task)”。进程是表示资源分配的基本单位。而线程则是进程中执行运算的最小单位,即执行处理机调度的基本单位。关于...
    99+
    2023-05-31
    线程 基本概念 并发
  • Java 虚拟机(JVM)之基本概念详解
    1、类加载子系统:负责从文件系统或者网络中加载Class信息,加载的信息存放在一块称之为方法区的内存空间。2、方法区:就是存放类信息、常量信息、常量池信息、包括字符串字面量和数字常量等。方法区是辅助堆栈的块永久区,解决堆栈信息的产生,是先决...
    99+
    2023-05-31
    java 虚拟机 基本概念
  • 详解docker容器的层的概念
    目录01 容器的一致性02 层的概念03 分层设计的优点    今天我们看看容器的层的概念。     上一节中,我们知道了...
    99+
    2022-11-12
  • 详解OpenCV图像的概念和基本操作
    前言: opencv最主要的的功能是用于图像处理,所以图像的概念贯穿了整个opencv,与其相关的核心类就是Mat。 像素: 图片尺寸以像素为单位时,每一厘米等于28像素,如15...
    99+
    2022-11-12
  • vuex的核心概念和基本使用详解
    目录介绍开始安装 ①直接下载方式 ②CND方式③NPM方式④Yarn方式NPM方式安装的使用方式 store概念及使用概念:定义使用mutations概念及使用概念:使用:定义使用a...
    99+
    2022-11-12
  • C#Marshal类基本概念和入门实例讲解
    目录C# Marshal类基本概念及入门C# Marshal.Copy实现非托管指针和数组之间的转换挑战写文件读文件总结C# Marshal类基本概念及入门 marshal:直译为...
    99+
    2023-02-26
    C# Marshal类 Marshal类基本概念 Marshal类入门
  • VB.NET变量生存周期的基本概念详解
    这篇文章主要讲解了“VB.NET变量生存周期的基本概念详解”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“VB.NET变量生存周期的基本概念详解”吧!VB.NET变量生存周期 这一点程序员们都...
    99+
    2023-06-17
  • C++BloomFilter布隆过滤器应用及概念详解
    目录一、布隆过滤器概念二、布隆过滤器应用三、布隆过滤器实现1.插入2.查找3.删除四、布隆过滤器优缺五、结语一、布隆过滤器概念 布隆过滤器是由布隆(Burton Howard Blo...
    99+
    2023-03-08
    C++ BloomFilter布隆过滤器 C++ BloomFilter C++布隆过滤器
  • Oracle中的12C新特性-容器数据库概念-基本操作
    Oracle 中的12C新特性-容器数据库 ---12.1.0.2_GI_RAC_Create_CDB_Blog 三月份的时候杰哥跟我讲有时间部署 Oracle 12C-...
    99+
    2022-10-18
  • 音视频基本概念和FFmpeg的简单入门教程详解
    目录写在前面基本概念容器/文件(Conainer/File):媒体流(Stream):数据帧/数据包(Frame/Packet):编解码器(Codec):复用(mux):解复用(mu...
    99+
    2022-11-13
  • 三大关系型数据库事务详解之一:基本概念
    一、基本概念   假设用户A要从他的账户里面给B转账1000元,那么就需要两步来实现,首先从A的账号减去1000元,再给B账号加1000元。这两个步骤中,任何一步都不能少或者出错,这两步要么都得到成功操作完成,要么什么都不做,中途...
    99+
    2020-09-12
    三大关系型数据库事务详解之一:基本概念
  • 45了解容器编排工具 Kubernetes 的基本概念和应用,包括 Pod、Service
    Kubernetes 是一种用于自动部署、扩展和管理容器化应用程序的开源容器编排工具。它可以轻松地管理和编排应用程序容器,确保它们在大规模的云环境中高效、可靠地运行。本教程将介绍 Kubernetes 的基本概念和应用,包括 Pod 和 ...
    99+
    2023-10-20
    运维 运维开发 服务器
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作