C++中string使用

【C++】std::string和size()函数进阶解析 C++ 中的 std::string 提供了一种高效且简单的方式来操作字符串,不仅可以进行轻松的字符串连接,还能完成长度计算、字符访问和后缀处理等处理任务。本文将优化精进地分析 C++ 中的 std::string 和其采用的常见函数,尤其是 size() 函数,并提供相关优化解释和知识拓展。 2025.1.8-✍56min 阅读详情

所有解释已经在程序中标明,不再单独解释

代码:

//如何使用C++标准库类型中的string
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	//1、构造函数
	cout << "***********************string类的构造函数***********************" << endl;
	string nullString;  //空字符串
	string str1("Hello, string"); //给定常量字符串的一个拷贝
	string str2(str1); //给定字符串的拷贝
	string str3(10, 'c'); //连续十个字符'c'
	string str4("Hello, string", 5); //取前五个字符
	string str5(str1, 7, 6);//从下标(下标从0开始)为7的字符起,取6个字符

	vector<char> vec;
	vec.push_back('a');
	vec.push_back('b');
	vec.push_back('c');

	string str6(vec.begin(), vec.end()); //把input_iterator迭代器[start,end)间的字符拷贝给str6

	cout << nullString << endl;
	cout << str1 << endl;
	cout << str2 << endl;
	cout << str3 << endl;
	cout << str4 << endl;
	cout << str5 << endl;
	cout << str6 << endl;

	//2、string类的字符操作
	cout << "***********************string类的字符操作***********************" << endl;
	string str("0123456789");
	str[0] = 'a';   //[]不提供越界检查  对于特殊的str[11]输出‘ ’ (在VS2012中[]也提供了越界检查)
	str.at(1) = 'b'; //at()提供越界检查 对于特殊的str.at(11)提示越界
	cout << str << endl;  

	cout << str.data() << endl;  //data()返回的是一个非null终止的C字符数组,返回值类型为const char*
	cout << str.c_str() << endl;  //c_str返回一个以null终止的C字符串,返回类型为const char*

	char s[100];
	cout << str.copy(s, 20, 0) << endl;  //把str中从0开始的20个字符拷贝到字符数组s中,返回值为实际拷贝的字符数目
	cout << s << endl;  //没有'\0'结尾哦

	//3、string类的特性
	cout << "***********************string类的特性***********************" << endl;
	cout << str.capacity() << endl;
	cout << str.max_size() << endl; //string对象可以存放的最大字符串长度
	cout << str.size() << endl;//返回字符串尺寸
	cout << str.length() << endl;//返回字符串长度
	cout << str.empty() << endl; //判断str是否为空字符串,返回值为bool型

	str.resize(20, 'a'); //重设字符串str的尺寸为20,不足部分用'a’填充
	cout << str.size() << endl;
	cout << str.length() << endl;
	cout << str << endl;

	//4、string类输入
	cout << "***********************string类的输入***********************" << endl;
	cout << "string类重载operator>>用于输入,重载operator<<用于输出" << endl;
	string tmp;
	while (getline(cin, tmp))  //以Ctrl+Z结束输入,这个是从标准输入流cin中输入字符的,以换行符'\n'分开
	{
		cout << tmp << endl;
	}

	//5、string类赋值
	cout << "***********************string类的赋值***********************" << endl;
	string forAssign("forAssign");
	str = forAssign;  //赋值操作符
	const char* c_string = "c_string for c++ string";
	str.assign(c_string);  //用c类型字符串给str赋值
	str.assign(c_string, 8); //把c_string开头8个字符赋值给str
	cout << str << endl;

	str.assign(forAssign); //把字符串forAssign赋值给str
	str.assign(10, 'a'); //把十个字符'a'赋值给str
	cout << str << endl;
	str.assign(forAssign, 0, 4);//把forAssign从下标0开始的4个字符赋值给str
	cout << str << endl;
	str.assign(vec.begin(), vec.end()); //把迭代器[begin, end)中的部分赋值为str
	cout << str << endl;

	//6、string类连接
	cout << "***********************string类连接***********************" << endl;
	string tmpAppend("append");
	str += tmpAppend;
	str.append("nihao");
	char cAppend[] = "c string for append";
	str.append(cAppend);
	str.append(cAppend, 5);  //将cAppend前5个字符加到str后面
	str.append(tmpAppend);
	str.append(tmpAppend, 0, 5); //将tmpAppend从下标0开始的5个字符加到str后
	str.append(10, 'd');  //在str后加上10个字符'd'
	str.append(vec.begin(), vec.end());

	cout << str << endl;

	//7、string的比较
	cout << "***********************string的比较***********************" << endl;
	str1 = "for compare";
	str2 = "for compare";
	if(str1==str2) //运算符==,>,<,>=,<=,!=均被重载
	{
		cout << "str1==str2" << endl;
	}
	else
	{
		cout << "str!=str2" << endl;
	}

	//compare的返回值为1,0,-1
	cout << str1.compare(str2) << endl;
	cout << str1.compare(0, 3, str2) << endl; //str1从下标0开始的3个字符组成的字符串与str2比较
	cout << str1.compare(0, 3, str2, 0, 3) << endl; //str从下标0开始的3个字符组成的字符串与str2从下标0开始的3个字符组成的串比较

	char cCompare[] = "for compare";
	cout << str1.compare(cCompare) << endl;
	cout << str1.compare(0, 3, cCompare) << endl;
	cout << str1.compare(0, 3, cCompare, 0, 3) << endl;

	//7、string的子串
	cout << "***********************string的子串***********************" << endl;
	str = "I am sub string in main string";
	string subStr = str.substr(0, 15);
	cout << subStr << endl;

	//8、string的交换
	cout << "***********************string的交换***********************" << endl;
	str1 = "I am str1";
	str2 = "I am str2";
	str1.swap(str2);
	cout << str1 << endl;
	cout << str2 << endl;

	//9、string的查找
	cout << "***********************string的查找***********************" << endl;
	cout << "查找成功返回所在位置,查找失败返回string::npos = " << (int)string::npos << endl;
	str = "0123456789";
	cout << "从left侧查找:" << endl;
	cout << str.find('3', 0) << endl;
	cout << str.find("567", 0) << endl;
	cout << str.find('a', 0) << endl;
	cout << str.find("456a", 2, 3) << endl; //从str的下标2开始查找字符串"456a"前3个字符在str中的位置

	string tmpFind("789");
	cout << str.find(tmpFind, 0) << endl;

	cout << "从right侧查找:" << endl;
	cout << str.rfind('4', 6) << endl;//从下标6位置从后向前查找字符'4'
	cout << str.rfind("78", 8) << endl;//从下标8位置从后向前查找"7,8"
	cout << str.rfind('9', -1) << endl;//这个也验证了string::npos == -1

	cout << str.rfind("56789", 8, 2) << endl;//从str的下标8的位置从后向前查找"56789"前两个字符组成的字符串的位置
	cout << str.rfind(tmpFind, 7) << endl; //只要tmpFind的第一个字符出现在str字符串0-7范围内就可以找到

	cout << "find_first_of : 查找当前串中那个字符最先在后面给定的字符串中出现,返回第一个出现在当前串中的位置" << endl;
	str = "abcabc";
	cout << str.find_first_of('a', 0) << endl;//从str下标0开始查找字符'a'第一次出现的位置
	cout << str.find_first_of("tybh", 0) << endl; //查找str中第一个在"tybh"中出现的字符位置
	cout << str.find_first_of("bca", 0, 2) << endl;//从str下标0开始查找第一个出现在字符串"bca"前2个字符组成的字符串"bc"的位置
	cout << str.find_first_of(tmpFind, 0) << endl;

	cout << "find_first_not_of : 查找当前串中那个字符最先不出现在后面给定的字符串中出现,返回第一个不出现在当前串中的字符的位置" << endl;
	str = "abcabcdf";
	cout << str.find_first_not_of('a', 0) << endl;//从str下标0开始查找字符'a'第一次不出现的位置
	cout << str.find_first_not_of("atybh", 0) << endl; //查找str中第一个不在"tybh"中出现的字符位置
	cout << str.find_first_not_of("bca", 0, 3) << endl;//从str下标0开始查找第一个不出现在字符串"bca"前2个字符组成的字符串"bc"的位置
	cout << str.find_first_not_of(tmpFind, 0) << endl;

	cout << "find_last_of : 从后向前查找当前串中那个字符最先在后面给定的字符串中出现,返回第一个出现在当前串中的位置" << endl;
	str = "abcdeabc";
	cout << str.find_last_of('a', string::npos) << endl; //从后向前查找第一个出现字符‘a'的位置,也即最后一个'a'的位置
	cout << str.find_last_of("ab", string::npos) << endl; //从后向前查找第一出现在"ab"中的字符位置,也即最后一个出现在"ab"中的字符位置
	cout << str.find_last_of("abc", 7, 2) << endl; //从下标为7的字符向前查找第一个不在"abc"前两个字符组成的字符串中的字符位置
	cout << str.find_last_of(tmpFind, string::npos) << endl;

	cout << "find_last_not_of : 从后向前查找当前串中那个字符最先不在后面给定的字符串中出现,返回第一个不出现在当前串中的位置" << endl;
	str = "abcdfabc";
	cout << str.find_last_not_of('c', string::npos) << endl;
	cout << str.find_last_not_of("abc", string::npos) << endl;
	cout << str.find_last_not_of("abc", string::npos, 2) << endl;
	cout << str.find_last_not_of(tmpFind, string::npos) << endl;

	//10、string的替换
	cout << "***********************string的替换***********************" << endl;
	str = "0123456789";
	char* c_replace = "C_Replace";
	string cp_replace("CP_Replace");
	cout << str.replace(0, 4, c_replace) << endl;//从下标0开始删除4个字符,然后在下标0处 插入字符串c_replace
	str = "0123456789";
	cout << str.replace(0, 4, c_replace, 5) << endl;//从下标0开始删除4个字符,然后在下标0处 插入字符串c_replace的前5个字符
	str = "0123456789";
	cout << str.replace(0, 4, cp_replace) << endl;
	str = "0123456789";
	cout << str.replace(0, 4, cp_replace, 3, 4) << endl;//从下标0开始删除4个字符,然后在下标0处,插入cp_replace从下标3开始的四个字符
	str = "0123456789";
	cout << str.replace(0, 4, 10, 'a') << endl;//从下标0开始删除4个字符,然后在下标0处 插入10个字符'a'

	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, "123") << endl;
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, "1234", 2) << endl; //把str中[str.begin(), str.begin()+3)的字符替换为“1234”的前2个字符
	subStr = "123";
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, subStr) << endl;
	str = "abc67abc";
	cout << str.replace(str.begin(), str.begin()+3, 10, 'f') << endl;//把str中[str.begin(), str.begin()+3)的字符替换为10个字符'f'
	str = "abc67abc";
	cout << str.replace(str.begin(), str.end(), subStr.begin(), subStr.end()) << endl;
	cout << str.replace(str.begin(), str.end(), str.begin(), str.end()) << endl;

	//11、string的插入
	cout << "***********************string的插入***********************" << endl;
	str = "abcdefg";
	subStr = "123";
	cout << str.insert(0, "123") << endl;
	cout << str.insert(0, "123", 2) << endl;//从下标0开始插入字符串"123"的前2个字符
	cout << str.insert(0, subStr) << endl;
	cout << str.insert(0, subStr, 2, 1) << endl;//从下标0开始出入字符串subStr从下标2开始的1个字符

	cout << str.insert(0, 10, '0') << endl;
	str = "abcefg";
	str.insert(str.begin(), '1'); //返回的是出入后迭代器的位置
	cout << str << endl;

	str = "abcefg";
	str.insert(str.begin(), str.begin()+2, str.begin()+4); //在str.begin()处插入[str.begin()+2, str.begin()+4)的字符
	cout << str << endl;
	str.insert(str.begin(), 10, 'f'); //在str.begin()处插入10个'f'字符

	//12、string的删除
	cout << "***********************string的删除***********************" << endl;
	str = "0123456789";
	str.erase(str.begin(), str.begin()+3);
	str.erase(str.begin());
	str = "0123456789";
	cout << str.erase(0, 3) << endl;

	string::iterator ite = str.begin();

	system("pause");
	return 0;
}

参考: http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html

C++(03):string的length, size, capacity, resize, reserve 主要注意的是扩容后,string对象会分配新的内存空间,然后将原来的字符串拷贝到新的空间,并释放原有的空间,也就是说:字符串的内容还维持不变,但是保存字符串的空间可能已经发生了变化。当count的数量大于当前的size时,string对象保存的字符串长度会增长,如果传递了ch参数,则使用ch的字符来填充,如果没有指定ch,则使用'\0'进行填充。如果new_cap的值大于当前string对象的容量,则对string的容量进行扩容,扩容后的容量大于等于new_cap。可以看到字符串没有受到影响。 阅读详情

相关推荐

速腾RS-LiDAR-16激光雷达ROS环境搭建与点云可视化实战指南

本文提供速腾RS-LiDAR-16激光雷达在ROS环境下的完整搭建与可视化实战指南。从硬件连接、网络配置、驱动安装编译,到核心参数详解与Rviz点云可视化,手把手解决IP设置、坐标系对齐等常见问题,帮助开发者快速实现激光雷达三维点云数据的采集与显示。

weixin_29271263的博客 372

C++中求string类型字符串长度的三种方法

C++中求string类型的字符串长度以及length、size和strlen的本质区别

qq_43762434的博客 2万+

保姆级教程:Halcon 3D点云匹配与差异分析避坑指南(从采样到可视化)

本文提供了一份详细的Halcon 3D点云匹配与差异分析实战指南,涵盖从模型准备到可视化的全流程。重点解析了STL文件读取单位误区、点云采样密度优化、表面模型创建技巧以及差异分析的专业手法,帮助用户避开常见陷阱,提升工业检测效率与精度。

weixin_33728268的博客 402

C++ 获取string字符串长度的三种方法

1.用string的成员方法length()获取字符串长度 length()比较直观,表示的就是该字符串的长度。 #include <string> #include <iostream> using namespace std; int main() { string str = "my string"; cout << str.length() << endl; return 0; }

Kallou的博客 3万+

NSGA-III相关笔记

NSGA-III目录目的区别NSGA-III的算法流程非支配排序详解归一化详解 目录 NSGA-III基于matlab的实现 参考文献 1.An Evolutionary Many-Objective Optimization Algorithm Using Reference-point Based Non-dominated Sorting Approach,Part I: Solving P...

weixin_42555400的博客 1万+

innovus:给channel加soft blockage

dbGet [dbGet top.fplan.pBlkgs.name finishfp_place_blkg_* -p].boxes就可以获得channel坐标。意思是给30um以下的channel加soft blk,至于channel的坐标,通过抓取该soft blk去获取,比较容易。工具自己加入的placement blockage命名上叫finishfp_place_blkg_*ICC2中有给channel自动加soft blockage的命令,相应的innovus也有类似的命令,我们可以使用。

m0_61544122的博客 488

c++基础(6) - string

简介: string 是 C++ 中常用的一个类,它非常重要,我们有必要在此单独讲解一下。 使用 string 类需要包含头文件<string>,下面的例子介绍了几种定义 string 变量(对象)的方法: ​ #include<iostream> #include<string> using namespace std; int main(){ string s1; string s2 ="c plus plus"; ...

不忘初心 1347

UG NX二次开发基础篇-外部模式与DLL内部模式(二 DLL内部模式)

通常情况下,我们希望我们的外挂,在UG启动的时候,就自动加载我们的外挂,并以菜单和工具条的方式,显示在UG上面。当然,这也是市面上所有外挂的操作,因为您不可能要求你的用户,以手工加载的方式,去调用你的外挂吧。在上一篇中,有粉丝留言说,启动 bat 文件,启动EXE外挂,会提示无法找到DLL的错误,这是因为UG的环境变量设置有问题,这里,我们把BAT文件作一个修改,直接改为实际目录。EXE外部模式,还有很多部分需要添加的,如参数的传递、输出消息的显示、UG操作的消息显示,等等。Dll执行结束后,会调用。

u014657217的博客 1656

如何在Dev-Cpp中使用C++11中的函数:stoi、to_string、unordered_map、unordered_set、auto

如果想要在Dev-Cpp里面使用C++11特性的函数,比如刷算法中常用的stoi、to_string、unordered_map、unordered_set、auto这些,需要在设置里面让dev支持c++11~需要这样做~ 在工具-编译选项-编译器-编译时加入这个命令“-std=c++11”:   然后就可以愉快的用这些好用到飞起的C++11函数啦啦啦啦啦啦~~~...

柳婼 の blog 1万+

天猫精灵设备拆机及系统分析

我手头的这个天猫精灵型号为TG_X1,黑色天猫精灵AI助手,拆解没有多少难度,但需要注意连接线的问题。具体的拆机过程可以参见:http://www.geekifix.com/wp/archives/415.html注意一点就是,天猫精灵的主板的位置,位于音频按键的底部,也就是顶端,而拆解是从电源的位置从低到顶进行拆机;拆机后,看到主控板的信息如下:主控型号:联发科 MT8516MT8516支持四核...

LeeKwen的专栏 3万+

【Python进阶】VSCode Python开发完全指南:从环境配置到高效调试

本文通过15个实战案例,系统讲解VSCode进行Python开发的环境配置、编码技巧、调试方法及性能优化,帮助初学者快速构建专业开发环境。

赶紧的博客 3779

C++_认识STL及使用及实现第一个容器string

不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架。

心动的那一刻,记忆便盛开了花 1001

冠层分析法提取叶面积指数及其应用

冠层分析法是利用植物冠层空间结构分析LAI的方法。其基本原理是通过相机或扫描仪对植物冠层垂直方向的照片或图像进行获取,再运用图像处理方法实现植被的自动识别和覆盖度计算,以此推算LAI。假设有一块田地,需测量其LAI,首先在田地中央设置一杆高度为3米、朝向田地中心、覆盖角度为50度的相机。利用上述函数,对相机采集到的图像进行处理,即可得到田地的LAI。在Matlab中,读取图片可以使用imread函数,对图片进行预处理可以使用imadjust函数调整灰度,使用imbinarize函数进行二值化处理。

m0_47037246的博客 739

【C++】string类的使用

在前面我们说过,前面的绝大部分内容都是在为了后面真正进入C++这块大门做铺垫,今天我们将正式的步入。

m0_64826370的博客 2263

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

【C++】string接口使用

string接口使用

m0_74452600的博客 1277

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Cpp中string使用

Cppstring的使用:创建初始化,赋值,使用函数等相关内容。 #include <string> #include <cstring> #include <string.h>

Aslixzhou 5010

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

【C++】C++ STL 探索:String的使用与理解

这篇文章将带大家深入探讨C++ STL中的string使用与理解。在接下来的几篇文章中,我们将介绍STL中相关知识点。掌握STL将使我们在使用C++编写代码时更加得心应手。

专注于 C/C++ 后端开发,涵盖基础语法、Linux系统与网络、MySQL数据库、常见算法及项目经验。 3977

C++中string类的使用

目录1.auto和范围for1.1auto关键字1.2范围for2.string类常用接口说明2.1默认成员函数2.1.1构造函数(constructor)2.1.2赋值运算符重载(operator=())2.2string类对象的访问及遍历操作(Iterators and Element access)2.3string类对象的容量操作(Capacity) 2.3.1利用reserve提高插入数据的效率2.4string类对象的修改及相关操作(Modifiers and String operations

CSQCSQCC的博客 1555

C++ [STL之string的使用]

字符串在程序中经常出现,C语言为此提供了很多字符串操作函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问,于是STL单独为字符串实现了一个容器,用来专门存储操作字符串,本文将介绍string的常用接口,有了string的加入,我们对字符串的操作将游刃有余!

ARMCSKGT的博客 6205

c++string使用

本文系统介绍了C++中的string类及其相关操作。string是basic_string<char>类的typedef重命名,可视为字符顺序表,其底层包含字符数组和'\0'结束符。文章详细阐述了string的构造/析构函数、多种访问方式(包括[]运算符、迭代器和范围for)、空间管理接口(如size/capacity/resize/reserve等)、元素访问方法(at/back/front等)以及插入删除操作(push_back/append/insert/erase等)。此外还介绍了字符串

2401_89521529的博客 2021

C++ 带你吃透string容器的使用

C++ 带你吃透string容器的使用和常见的应用场景

Wzs040810的博客 2577

【C++】string的使用

讲解了C++中string的常用接口,以及string头文件中一些常见函数的使用,学会提前拓宽容量优化代码效率✨✨

Lin_Alpaca的博客 2792

【C++】string的使用【下】

本期博客带你了解并会使用string的常见操作~

2401_86785052的博客 1379

【C++进阶篇】——string类的使用

是 C++ 标准库的一部分,但它不是 STL 容器的一部分。STL 容器是指那些基于模板的容器,如std::list等。提供了类似于 STL 容器的功能,比如动态内存管理、迭代器支持等,但它的设计和实现是独立的。被归为 STL 是因为它具有类似于 STL 容器的特性和功能,尽管它在技术上并不属于 STL。这种归类更多是出于功能和使用上的相似性,而不是严格的分类。是 C++ 标准库中的一个类,它提供了一种方便的方式来创建、操作和处理字符串。

zoelinkailuo的博客 1848
上一篇: UDP协议
下一篇: 单例模式C++实现
知行且执行
博客等级 码龄14年 38粉丝 73原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值