C++ string类详解

这篇具有很好参考价值的文章主要介绍了C++ string类详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

⭐️ string

string 是表示字符串的字符串类,该类的接口与常规容器的接口基本一致,还有一些额外的操作 string 的常规操作,在使用 string 类时,需要使用 #include <string> 以及 using namespace std;

帮助文档:https://cplusplus.com/reference/string/string/string/

🌟 std::string::string 构造函数(constructor)

序号 构造函数 功能
1️⃣ string() 默认拷贝:构造空的string类对象,即空字符串,默认第一个字符位置是'\0',为了兼容c
2️⃣ string(const string& str) 拷贝构造
3️⃣ string(const string& str , size_t pos , size_t len = npos) 拷贝构造的重载,从字符串 pos 位置开始拷贝,拷贝 len 个字符
4️⃣ string(const char * s) 使用 c_string 来初始化 string 类对象
5️⃣ string(const char * s , size_t n) s 指向的字符串中复制前 n 个字符
6️⃣ string(size_t n , char c) 使用 nc 字符来填充字符串
7️⃣ template <class InputIterator> string (InputIterator first , InputIterator last) 复制一个迭代器序列的字符串 [first , last)
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s1;	// 默认构造 第一个字符是 `\0`
	cout << s1 << endl;

	/*
		调用构造本身的写法应该是:string s2("hello world");
		但是为什么可以这样写呢? string s2 = "hello world";
		因为当构造函数是单参的时候,是支持隐式类型转换的。这里本质上先构造再拷贝构造,但是编译器
		通常会优化为直接构造。若不希望隐式类型的转换可以在构造函数前添加 explicit 关键字
	*/
	string s2 = "hello world"; // 使用c字符串构造一个string类对象
	cout << s2 << endl;

	string s3(s2);	// 拷贝构造
	cout << s3 << endl;

	string s4(s3, 6, 5);  // 拷贝构造的重载 从下标6位置开始拷贝 拷贝5个字符
	cout << s4 << endl;

	string s5("hello" , 3); // 拷贝 `hello` 前3个字符
	cout << s5 << endl;

	string s6(10 , 'a'); // 使用 10个 `a` 填充对象
	cout << s6 << endl;

	string s7(s6.begin() , s6.end()); // 迭代器序列初始化
	cout << s7 << endl;


	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


ps: npos 原型:static const size_t npos = -1;npos 的访问:string::npos。无符号的 -1 相当于是整型的最大值,若当不传这个参数时, 或者 len 大于后面剩余字符的长度,那么使用默认参数 npos都是相当于 直到字符串的结束。[ 返回 ]

C++ string类详解,c++,c++,开发语言,学习


🌟 std::string::operator= 赋值重载

序号 函数名称 功能
1️⃣ string& operator= (const string& str) 用一个新的 string 对象替换当前 string 对象的内容
2️⃣ string& operator= (const char * s) 用一个c的字符串替换当前 string 对象内容
3️⃣ string& operator= (char c) 使用一个字符替换当前 string 对象的内容
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s1 = "hello world";
// 1.
	string temp = "ccc";
	s1 = temp;
	cout << s1 << endl;
// 2.
	s1 = "hhhh";
	cout << s1 << endl;
// 3.
	s1 = 'a';
	cout << s1 << endl;

	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


🌟 元素的访问

序号 函数名称 功能
1️⃣ char& operator[] (size_t pos) 返回当前 string 对象中 pos 位置字符的引用
2️⃣ const char& operator[](size_t pos) const 返回当前 const string 对象中 pos 位置字符的引用
3️⃣ char& at (size_t pos) 返回当前 string 对象中 pos 位置字符的引用
4️⃣ const char& at (size_t pos) const 返回当前 const string 对象中 pos 位置字符的引用
5️⃣ char& back() 返回当前 string 对象最后一个字符的引用
6️⃣ const char& back() const 返回当前 const string 对象最后一个字符的引用
7️⃣ char& front() 返回当前 string 对象第一个字符的引用
8️⃣ const char& front() const 返回当前 const string 对象第一个字符的引用

ps: at[] 的行为是一样的,函数都会检查 pos 是否是合法位置,若不是,[] 是断言错误,而 at 是抛异常。

ps: back == [xx.size() - 1]front == [0]

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

int main() {

	const string s1 = "hello";
	for (size_t i = 0; i < s1.size(); i++) {
		cout << s1[i] << " ";
	}

	cout << endl;

	string s2 = "world";
	for (size_t i = 0; i < s2.size(); i++) {
		s2[i]++;
		cout << s2[i] << " ";
	}

	cout << endl;

	string s3 = "hello world";
	cout << s3.back() << s3[s3.size() - 1] << endl;
	cout << s3.front() << s3[0] << endl;
	cout << s3.at(4) << endl;

	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


🌟 元素的长度

序号 函数名称 功能
1️⃣ size_t size() const 返回 string 对象实际字符的长度
2️⃣ size_t length() const 返回 string 对象实际字符的长度
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s = "hello world";
	cout << s.size() << endl;	// 11
	cout << s.length() << endl;	// 11

	return 0;
}

🌟 string 迭代器

序号 函数名称 功能
1️⃣ iterator begin() 返回一个迭代器,该迭代器指向 string 对象的第一个字符
2️⃣ const_iterator begin() const 返回一个迭代器,该迭代器指向 const string 对象的第一个字符
3️⃣ iterator end() 返回一个迭代器,该迭代器指向 string 对象最后一个实际字符的下一个位置
4️⃣ const_iterator end() const 返回一个迭代器,该迭代器指向 const string 对象最后一个实际字符的下一个位置
5️⃣ reverse_iterator rbegin() 返回一个反向迭代器,该迭代器指向 string 对象最后一个实际字符的位置
6️⃣ const_reverse_iterator rbegin() const 返回一个反向迭代器,该迭代器指向 const string 对象最后一个实际字符的位置
7️⃣ reverse_iterator() rend() 返回一个反向迭代器,该迭代器指向 string 对象第一个字符的前一个位置
8️⃣ const_reverse_iterator() rend() const 返回一个反向迭代器,该迭代器指向 const string 对象第一个字符的前一个位置

ps: [ begin() , end() )( rend() , rbegin() ]

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

int main() {

	string s = "hello world";
	for (string::iterator it = s.begin(); it != s.end(); it++) {
		cout << *it << " ";
	}
	// output: h e l l o   w o r l d
	cout << endl;

	// 自动迭代 自动判断结束
	// 范围for 本质上调用的也是迭代器
	for (auto val : s) {
		cout << val << " ";
	}
	// output: h e l l o   w o r l d
	cout << endl;

	for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++) {
		cout << *it << " ";
	}
	// output: d l r o w   o l l e h
	cout << endl;

	// const 
	const string s2 = "nihao";
	string::const_iterator it = s2.begin();
	while (it != s2.end()) {
		cout << *it << " ";
		it++;
	}
	// output: n i h a o

	cout << endl;
	string::const_reverse_iterator rit = s2.rbegin();
	while (rit != s2.rend()) {
		cout << *rit << " ";
		rit++;
	}
	// output: o a h i n

	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


🌟 string 对象修改

序号 函数名称 功能
1️⃣ void push_back (char c) 在当前 string 对象的末尾追加一个 c 字符
2️⃣ string& append (const string& str) 在当前 string 对象的末尾追加一个 const string str 对象
3️⃣ string& append (const string& str , size_t subpos , size_t sublen) 在当前 string 对象的末尾追加一个 const string str 对象的子串,从 subpos 位置开始,拷贝 sublen 个字符过去
类似上面构造函数的 npos 用法
4️⃣ string& append (const char* s); 在当前 string 对象的末尾追加一个 c_string 字符串
5️⃣ template <class InputIterator> string& append (InputIterator first , InputIterator last) 追加一个迭代器序列的字符串 [first , last)
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s = "hello";
	s.push_back('-');
	cout << s << endl;

	s = "hello";
	string temp = " world";
	s.append(temp);
	cout << s << endl;

	string s2 = "hello";
	string temp2 = " world";
	s2.append(temp2 , 0 , 3);
	cout << s2 << endl;

	string s3 = "hello";
	s3.append(" world");
	cout << s3 << endl;

	string s4 = "hello";
	s4.append(s4.begin(), s4.end());
	cout << s4 << endl;

	string s5 = "hello";
	s5.append(s5.rbegin() , s5.rend());
	cout << s5 << endl;

	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


std::string::operator+= 运算符重载

序号 函数名称 功能
6️⃣ string& operator+= (const string& str); 在当前 string 对象的末尾追加一个 const string str 对象
7️⃣ string& operator+= (const char* s); 在当前 string 对象的末尾追加一个 c_string 字符串
8️⃣ string& operator+= (char c); 在当前 string 对象的末尾追加一个 c 字符
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s = "he";
	s += 'l';
	s += 'l';
	s += "o ";
	string temp = "world";
	s += temp;
	cout << s << endl;
	
	// output: hello world

	return 0;
}

🌟 元素的容量

序号 函数名称 功能
1️⃣ size_t capacity() const 返回当前 string 对象的容量大小
2️⃣ void reserve (size_t n = 0) 改变当前 string 对象的容量为 n
3️⃣ void resize (size_t n) 将当前 string 对象的 size() 调整为 n 并初始化为 '\0'
4️⃣ void resize (size_t n , char c) 将当前 string 对象的 size() 调整为 n 并初始化为 c
5️⃣ void clear(); 删除当前 string 对象的所有内容,size() = 0
6️⃣ bool empty() const 若当前 string 对象为空返回 true,否则返回 false

ps: reserve 是改变容量,而 resize 是改变 size() + 初始化,resizen 传的比 string 的大小还小,则就是删除。

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

int main() {

	string s = "hello";
	cout << s.capacity() << endl;
	s.reserve(100);
	cout << s.capacity() << endl;
	cout << s.size() << endl; // 5
	
	string s2 = "hello world";
	s2.resize(5);
	cout << s2.size() << endl;	// 100
	cout << s2 << endl;
	s2.clear();
	cout << s2.empty() << endl;


	return 0;
}

output:

C++ string类详解,c++,c++,开发语言,学习


🌟 std::string::insert 插入

C++ string类详解,c++,c++,开发语言,学习


ps: 需要的查文档即可,效率不高很少用。

🌟 std::string::erase 删除

C++ string类详解,c++,c++,开发语言,学习


🌟 std::string::c_str 返回c的字符串

序号 函数名称 功能
1️⃣ const char* c_str() const 返回c的字符串使用 '\0' 结尾

🌟 查找

序号 函数名称 功能
1️⃣ size_t find (char c , size_t pos = 0) const 从当前 string 对象的 pos 位置开始查找 c 字符,返回这个字符第一次出现的位置,否则返回 string::npos
2️⃣ string substr (size_t pos = 0 , size_t len = npos) const 返回当前对象 pos 位置开始的 len 个字符的子串
#include <iostream>
#include <string>
using namespace std;

int main() {

	string s = "hello world";
	size_t res = s.find('w' , 0);
	if (res != string::npos) {
		cout << s.substr(res) << endl;	// world
	}

	return 0;
}
序号 函数名称 功能
3️⃣ size_t rfind (char c , size_t pos = npos) const 从当前 string 对象的 pos 位置从后向前开始查找 c 字符,返回这个字符最后一次出现的位置,否则返回 string::npos

🌟 其他

序号 函数名称 功能
1️⃣ istream& getline (istream& is , string& str , char delim) 输入一行字符遇到 delim 终止
2️⃣ istream& getline (istream& is , string& str) 输入一行字符遇到 \n 终止
3️⃣ string to_string (int val) 返回一个 valstring 对象
4️⃣ int stoi (const string& str, size_t* idx = 0, int base = 10) 字符串转整数。 idx 通常都为 nullptrbase 代表进制

ps: to_string 支持的转换类型

C++ string类详解,c++,c++,开发语言,学习


ps: string 可以转换为的类型

C++ string类详解,c++,c++,开发语言,学习文章来源地址https://www.toymoban.com/news/detail-659757.html


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

int main() {

	int val = 10;
	string s_val = to_string(val);
	cout << s_val << endl;	 // 10
	

	val = stoi(s_val);
	cout << val << endl;	// 10

	return 0;
}

到了这里,关于C++ string类详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++】STL——string(两万字详解)

    🎇C++学习历程:STL——string学习 博客主页: 一起去看日落吗 持续分享博主的C++学习历程 博主的能力有限,出现错误希望大家不吝赐教 分享给大家一句我很喜欢的话: 也许你现在做的事情,暂时看不到成果,但不要忘记,树🌿成长之前也要扎根,也要在漫长的时光🌞中沉

    2024年01月25日
    浏览(36)
  • C++ string类详解及模拟实现

    目录 【本节目标】  1. 为什么学习string类? 1.1 C语言中的字符串  1.2 面试题(暂不做讲解)  2. 标准库中的string类 2.1 string类(了解)  2.2 string类的常用接口说明(注意下面我只讲解最常用的接口) 3. string类的模拟实现 3.1string类常用成员函数的模拟实现 3.2 浅拷贝和深拷贝   

    2024年03月23日
    浏览(23)
  • C++STL详解 string【C++】

    函数模板是一个蓝图,它本身并不是函数,是编译器用使用方式产生特定具体类型函数的模具。所以其实模板就是将本来应该我们做的重复的事情交给了编译器 在编译器编译阶段,对于模板函数的使用,编译器需要根据传入的实参类型来推演生成对应类型的函数以供调用。比

    2024年02月08日
    浏览(45)
  • 【C++入门】string类常用方法(万字详解)

    1.STL简介 1.1什么是STL STL(standard template libaray-标准模板库):是C++标准库的重要组成部分,不仅是一个可复用的组件库,而且是一个包罗数据结构与算法的软件框架 。 1.2STL的版本 原始版本 Alexander Stepanov、Meng Lee 在惠普实验室完成的原始版本,本着开源精神,他们声明允许任何

    2024年02月10日
    浏览(28)
  • 【C++ • STL • 力扣】详解string相关OJ

    ヾ(๑╹◡╹)ノ\\\" 人总要为过去的懒惰而付出代价 ヾ(๑╹◡╹)ノ\\\" 力扣链接 代码1展示 :【下标】 代码2展示 :【迭代器】 思路 :快速排序中的单趟排序 知识点 :C++库提供了swap的函数,可以直接调用。 力扣链接 代码展示 : 思路 :计数排序的思想 牛客链接 代码展示 :

    2024年02月09日
    浏览(29)
  • C++学习 --string

    目录 1, 什么是string 2, 创建string 2-1,普通创建  2-2, 拷贝构造创建 2-2-1, 整体拷贝 2-2-2, 部分拷贝 2-3, 指定长度创建 3, 赋值(assign) 3-1, 拷贝构造赋值 3-1-1, 整体赋值 3-1-2, 部分赋值 3-2, 指定长度赋值 4, 拼接 4-1, 拼接(+) 4-2, 拼接(append) 4-2-1, 整体拼接 4-2-2,

    2024年02月04日
    浏览(50)
  • 【C++深入浅出】STL之string用法详解

    目录 一. 前言 二. STL概要 2.1 什么是STL 2.2 STL的六大组件 2.3 STL的缺陷 三. string类概述 3.1 什么是string类 3.2 为什么要使用string类 四. string类的使用 4.1 包含头文件 4.2 构造函数 4.3 赋值运算符重载 4.4 容量操作 4.5 访问/遍历操作 4.6 查找修改操作 4.7 子串操作 ​4.8 非成员函数  

    2024年02月05日
    浏览(36)
  • 【C++ STL之string,tuple,array详解】

    在C++的STL(Standard Template Library)中,std::string是一个非常有用的字符串类。它提供了一系列操作字符串的功能,包括字符串的创建、修改、查找、拼接等。本文将详细介绍C++ STL中std::string的使用方法和一些常见操作。 (1) 支持比较运算符 string字符串支持常见的比较操作符(

    2024年02月12日
    浏览(40)
  • 【C++】对于string的学习

    本篇文章针对C++中的string做一些详细的介绍和学习,并且会包含一些关于STL的介绍 注意: 本篇文章只包含基础知识 STL是标准模板库的一个缩写,全称是Standard Template Library C++中定义了一种类,为string,存放在头文件string中 存放C语言风格字符串的头文件是cstring,也就是以’

    2024年02月10日
    浏览(33)
  • 【C++初阶】STL详解(二)string类的模拟实现

    本专栏内容为:C++学习专栏,分为初阶和进阶两部分。 通过本专栏的深入学习,你可以了解并掌握C++。 💓博主csdn个人主页:小小unicorn ⏩专栏分类:C++ 🚚代码仓库:小小unicorn的代码仓库🚚 🌹🌹🌹关注我带你学习编程知识 注:为了防止与标准库当中的string类产生命名冲

    2024年02月05日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包