【C++】——string类的介绍及模拟实现

news2024/9/21 4:24:00

文章目录

  • 1. 前言
  • 2. string类的常用接口
    • 2.1 string类对象的常见构造
    • 2.2 string类对象的容量操作
    • 2.3 string类对象的访问及遍历操作
    • 2.4 string类对象的修改操作
    • 2.5 string类非成员函数
    • 2.6 string四种迭代器类型
    • 2.7 string类的insert和erase函数
  • 3. 浅拷贝和深拷贝
  • 4. string类模拟实现
  • 5. 结尾

1. 前言

C语言中,字符串是以’\0’结尾的一些字符的集合,为了操作方便,C标准库中提供了一些str系列的库函数,但是这些库函数与字符串是分离开的,不太符合OOP的思想,而且底层空间需要用户自己管理,稍不留神可能还会越界访问。所以我们今天来学习C++标准库中的string类。

2. string类的常用接口

2.1 string类对象的常见构造

(constructor)函数名称功能说明
string()构造空的string类对象,即空字符串
string(const char* s)用C-string来构造string类对象
string(size_t n, char c)string类对象中包含n个字符c
string(const string&s)拷贝构造函数
void Test1()
{
	string s1;
	//string s2 = "hello world!!!";
	string s2("hello world!!!");
	cout << s1 << endl;
	cout << s2 << endl;

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

	string s4(s2, 6, 5);
	cout << s4 << endl;

	//第三个参数len大于后面字符长度,有多少拷贝多少拷贝到结尾
	string s5(s2, 6, 15);
	cout << s5 << endl;
	string s6(s2, 6);
	cout << s6 << endl;

	string s7("hello world", 5);
	cout << s7 << endl;

	string s8(100, 'x');
	cout << s8 << endl;

}

在这里插入图片描述

2.2 string类对象的容量操作

函数名称功能说明
size返回字符串有效字符长度
length返回字符串有效字符长度
capacity返回空间总大小
empty检测字符串释放为空串,是返回true,否则返回false
clear清空有效字符
reserve为字符串预留空间
resize将有效字符的个数该成n个,多出的空间用字符c填充

注意
注意:
1.size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接口保持一致,一般情况下基本都是用size()。
2.clear()只是将string中有效字符清空,不改变底层空间大小。
3.resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变。
4.reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参数小于string的底层空间总大小时,reserver不会改变容量大小。

2.3 string类对象的访问及遍历操作

函数名称功能说明
operator[]返回pos位置的字符,const string类对象调用
begin+ endbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
rbegin + rendbegin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器
范围forC++11支持更简洁的范围for的新遍历方式
void Test3()
{

	string s1("hello world");
	cout << s1[0] << endl;
	s1[0] = 'x';
	cout << s1[0] << endl;
	cout << s1 << endl;

	//遍历s1,每个字符+1
	for (size_t i = 0; i < s1.size(); i++)
	{
		s1[i]++;
	}
	cout << s1 << endl;

	const string s2("world");
	for (size_t i = 0; i < s2.size(); i++)
	{
		//s2[i]++; 失败,const修饰的变量不能修改
		cout << s2[i] << " ";
	}
	cout << endl;
	cout << s2 << endl;
	//cout << s2[6] << endl; 失败,operator[]内部会检查越界访问

}

在这里插入图片描述

void Test4()
{
	string s1("hello");
	//iterator迭代器类型,像指针一样的类型,有可能就是指针,也可能不是指针,但用法和指针一样
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		(*it)++;
		cout << *it << " ";
		it++;
	}
	cout << endl;
	cout << s1 << endl;

	//范围for,自动迭代,自动判断结束
	//依次取s1的每个字符,赋值给ch
	//for (auto ch : s1)
	//{
	//	ch++;//改变不会影响s1
	//	cout << ch << " ";
	//}
	//cout << endl;
	//cout << s1 << endl;
	for (auto& ch : s1)
	{
		ch++;
		cout << ch << " ";
	}
	cout << endl;
	cout << s1 << endl;


	list<int> lt(10, 1);
	list<int>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		lit++;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	//范围for底层其实就是迭代器

}

在这里插入图片描述

2.4 string类对象的修改操作

函数名称功能说明
push_back在字符串后尾插字符c
append在字符串后追加一个字符串
operator+=字符串后追加字符串str
c_str返回C格式字符串
find+npos从字符串pos位置开始往后找字符c,返回该字符在字符串中的位置
rfind从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
substr在str中从pos位置开始,截取n个字符,然后将其返回
void Test6()
{
	string s1("hello");
	s1.push_back('-');
	s1.push_back('-');
	s1.append("world");
	cout << s1 << endl;

	string s2("哈哈哈");
	s1 += '@';
	s1 += s2;
	s1 += "!!!";
	cout << s1 << endl;

	s1.append(++s2.begin(), --s2.end());
	cout << s1 << endl;

	//string copy(++s1.begin(), --s1.end());
	string copy(s1.begin() + 5, s1.end() - 5);
	cout << copy << endl;

}

在这里插入图片描述

void Test8()
{
	string s1("hello world");
	cout << s1 << endl;
	cout << s1.c_str() << endl;

	s1 += '\0';
	s1 += "hello";
	cout << s1 << endl;//string对象以size为结束的标准
	cout << s1.c_str() << endl;//常量字符串对象以\0为结束的标准

}

在这里插入图片描述

void Test9()
{
	string s1("test.cpp");
	//取后缀
	size_t pos = s1.find('.');
	if (pos != string::npos)
	{
		string s2 = s1.substr(pos);
		cout << s2 << endl;
	}

	string s3("test.cpp.txt.zip");
	size_t pos2 = s3.rfind('.');
	if (pos2 != string::npos)
	{
		string s4 = s3.substr(pos2);
		cout << s4 << endl;
	}


}

在这里插入图片描述

2.5 string类非成员函数

函数名称功能说明
operator+尽量少用,因为传值返回,导致深拷贝效率低
operator>>输入运算符重载
operator<<输出运算符重载
getline获取一行字符串
relational operators大小比较
void Test10()
{
	string s1("aaabbb");
	string s2("aaabbc");
	cout << (s1 > s2) << endl;
	cout << (s1 < s2) << endl;

}

在这里插入图片描述

void Test11()
{
	string s1;
	getline(cin, s1);
	cout << s1 << endl;
}

在这里插入图片描述

2.6 string四种迭代器类型

iterator迭代器类型,像指针一样的类型,有可能就是指针,也可能不是指针,但用法和指针一样
string类一共有四种迭代器:
iterator/const_iterator
reverse_iterator/const_reverse_iterator
具体功能如下

void Test4()
{
	string s1("hello");
	//iterator迭代器类型,像指针一样的类型,有可能就是指针,也可能不是指针,但用法和指针一样
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		(*it)++;
		cout << *it << " ";
		it++;
	}
	cout << endl;
	cout << s1 << endl;

	//范围for,自动迭代,自动判断结束
	//依次取s1的每个字符,赋值给ch
	//for (auto ch : s1)
	//{
	//	ch++;//改变不会影响s1
	//	cout << ch << " ";
	//}
	//cout << endl;
	//cout << s1 << endl;
	for (auto& ch : s1)
	{
		ch++;
		cout << ch << " ";
	}
	cout << endl;
	cout << s1 << endl;


	list<int> lt(10, 1);
	list<int>::iterator lit = lt.begin();
	while (lit != lt.end())
	{
		cout << *lit << " ";
		lit++;
	}
	cout << endl;

	for (auto e : lt)
	{
		cout << e << " ";
	}
	cout << endl;
	//范围for底层其实就是迭代器

}

在这里插入图片描述

void PrintString(const string& str)
{
	//auto it = str.begin();
	string::const_iterator it = str.begin();
	while (it != str.end())
	{
		//*it = 'x'; 失败,不能修改const类型的变量
		cout << *it << " ";
		it++;
	}
	cout << endl;


	string::const_reverse_iterator rit = str.rbegin();
	while (rit != str.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

}


void Test5()
{
	string s1("hello");
	string::reverse_iterator rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

	PrintString(s1);

}

在这里插入图片描述

2.7 string类的insert和erase函数

insert:在pos位置,插入字符串或字符,原位置字符串向后挪动。
erase:从pos位置开始,删除后面的n个字符。

void Test7()
{
	string s1("哈 哈 哈");
	for (size_t i = 0; i < s1.size(); i++)
	{
		if (s1[i] == ' ')
		{
			s1.insert(i, "666");
			i += 3;
		}

	}
	cout << s1 << endl;

	for (size_t i = 0; i < s1.size(); i++)
	{
		if (s1[i] == ' ')
		{
			s1.erase(i, 1);
		}
	}
	cout << s1 << endl;

	string str;
	for (size_t i = 0; i < s1.size(); i++)
	{
		if (s1[i] != ' ')
		{
			str += s1[i];
		}
		else
		{
			str += "666";
		}
	}
	cout << str << endl;

}

在这里插入图片描述

3. 浅拷贝和深拷贝

浅拷贝:也叫位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被放,以为还有效,所以当继续对资源进项操作时,就会发生发生了访问违规。
深拷贝:如果一个类中涉及到资源的管理,其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。

4. string类模拟实现

namespace fiora
{
	class string
	{
	public:
		typedef char* iterator;
		typedef const char* const_iterator;

		iterator begin()
		{
			return _str;
		}

		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}

		const_iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
		{
			_size = strlen(str);
			_capacity = _size;
			_str = new char[_capacity + 1];

			strcpy(_str, str);
		}

		// 传统写法
		// s2(s1)
		//string(const string& s)
		//	:_str(new char[s._capacity+1])
		//	, _size(s._size)
		//	, _capacity(s._capacity)
		//{
		//	strcpy(_str, s._str);
		//}

		 s1 = s3
		 s1 = s1
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{
		//		char* tmp = new char[s._capacity + 1];
		//		strcpy(tmp, s._str);

		//		delete[] _str;

		//		_str = tmp;
		//		_size = s._size;
		//		_capacity = s._capacity;
		//	}

		//	return *this;
		//}

		// 现代写法
		// s2(s1)
		void swap(string& tmp)
		{
			::swap(_str, tmp._str);
			::swap(_size, tmp._size);
			::swap(_capacity, tmp._capacity);
		}

		// s2(s1)
		string(const string& s)
			:_str(nullptr)
			, _size(0)
			, _capacity(0)
		{
			string tmp(s._str);
			swap(tmp); //this->swap(tmp);
		}

		// s1 = s3
		//string& operator=(const string& s)
		//{
		//	if (this != &s)
		//	{
		//		//string tmp(s._str);
		//		string tmp(s);
		//		swap(tmp); // this->swap(tmp);
		//	}

		//	return *this;
		//}

		// s1 = s3
		string& operator=(string s)
		{
			swap(s);
			return *this;
		}

		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		const char* c_str() const
		{
			return _str;
		}

		size_t size() const
		{
			return _size;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		const char& operator[](size_t pos) const
		{
			assert(pos < _size);

			return _str[pos];
		}

		char& operator[](size_t pos)
		{
			assert(pos < _size);

			return _str[pos];
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);
				delete[] _str;

				_str = tmp;
				_capacity = n;
			}
		}

		void push_back(char ch)
		{
			// 满了就扩容
			/*if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';*/
			insert(_size, ch);
		}

		void append(const char* str)
		{
			//size_t len = strlen(str);

			 满了就扩容
			 _size + len  8  18  10  
			//if (_size + len > _capacity)
			//{
			//	reserve(_size+len);
			//}

			//strcpy(_str + _size, str);
			strcat(_str, str); 需要找\0,效率低
			//_size += len;
			insert(_size, str);
		}

		string& operator+=(char ch)
		{
			push_back(ch);
			return *this;
		}

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}

		string& insert(size_t pos, char ch)
		{
			assert(pos <= _size);

			// 满了就扩容
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			 挪动数据
			//int end = _size;
			//while (end >= (int)pos)
			//{
			//	_str[end + 1] = _str[end];
			//	--end;
			//}
			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			++_size;

			return *this;
		}

		string& insert(size_t pos, const char* str)
		{
			assert(pos <= _size);
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			// 挪动数据
			size_t end = _size + len;
			while (end >= pos + len)
			{
				_str[end] = _str[end - len];
				--end;
			}

			strncpy(_str + pos, str, len);
			_size += len;

			return *this;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);

			if (len == npos || pos + len >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str + pos, _str + pos + len);
				_size -= len;
			}
		}

		size_t find(char ch, size_t pos = 0) const;
		size_t find(const char* sub, size_t pos = 0) const;
		bool operator>(const string& s) const;
		bool operator==(const string& s) const;
		bool operator>=(const string& s) const;
		bool operator<=(const string& s) const;
		bool operator<(const string& s) const;
		bool operator!=(const string& s) const;
	private:
		size_t _capacity;
		size_t _size;
		char* _str;

		// const static 语法特殊处理
		// 直接可以当成定义初始化
		const static size_t npos = -1;
	};

	ostream& operator<<(ostream& out, const string& s)
	{
		for (size_t i = 0; i < s.size(); ++i)
		{
			out << s[i];
		}

		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		// 输入字符串很长,不断+=,频繁扩容,效率很低
		char ch;
		//in >> ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			s += ch;
			ch = in.get();
		}

		return in;
	}

	//size_t string::npos = -1;

	void test_string1()
	{
		/*std::string s1("hello world");
		std::string s2;*/
		string s1("hello world");
		string s2;

		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

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

		for (size_t i = 0; i < s1.size(); ++i)
		{
			s1[i]++;
		}

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

	void test_string2()
	{
		string s1("hello world");
		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << " ";
			++it;
		}
		cout << endl;

		it = s1.begin();
		while (it != s1.end())
		{
			*it += 1;
			++it;
		}
		cout << endl;

		for (auto ch : s1)
		{
			cout << ch << " ";
		}
		cout << endl;
	}

	void test_string3()
	{
		string s1("hello world");
		string s2(s1);
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		s2[0] = 'x';
		cout << s1.c_str() << endl;
		cout << s2.c_str() << endl;

		string s3("111111111111111111111111111111");
		s1 = s3;
		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;

		s1 = s1;

		cout << s1.c_str() << endl;
		cout << s3.c_str() << endl;
	}

	void test_string4()
	{
		string s1("hello world");
		string s2("xxxxxxx");

		s1.swap(s2);
		swap(s1, s2);
	}

	void test_string5()
	{
		string s1("hello");
		cout << s1.c_str() << endl;
		s1.push_back('x');
		cout << s1.c_str() << endl;
		cout << s1.capacity() << endl;

		s1 += 'y';
		s1 += 'z';
		s1 += 'z';
		s1 += 'z';
		s1 += 'z';
		s1 += 'z';
		s1 += 'z';
		cout << s1.c_str() << endl;
		cout << s1.capacity() << endl;
	}

	void test_string6()
	{
		string s1("hello");
		cout << s1.c_str() << endl;
		s1 += ' ';
		s1.append("world");
		s1 += "bit hello";
		cout << s1.c_str() << endl;

		s1.insert(5, '#');
		cout << s1.c_str() << endl;

		s1.insert(0, '#');
		cout << s1.c_str() << endl;
	}

	void test_string7()
	{
		string s1("hello");
		cout << s1.c_str() << endl;

		s1.insert(2, "world");
		cout << s1.c_str() << endl;

		s1.insert(0, "world ");
		cout << s1.c_str() << endl;
	}

	void test_string8()
	{
		string s1("hello");
		s1.erase(1, 10);
		cout << s1.c_str() << endl;


		string s2("hello");
		s2.erase(1);
		cout << s2.c_str() << endl;

		string s3("hello");
		s3.erase(1, 2);
		cout << s3.c_str() << endl;
	}

	void test_string9()
	{
		/*	string s1;
			cin >> s1;
			cout << s1 << endl;*/

		string s1("hello");
		cout << s1 << endl;
		cout << s1.c_str() << endl;
		s1 += '\0';
		s1 += "world";
		cout << s1 << endl;
		cout << s1.c_str() << endl;

		string s3, s4;
		cin >> s3 >> s4;
		cout << s3 << s4 << endl;
	}
}

5. 结尾

C++string类的基本概念我们就学习到这里,在常规工作中,为了简单、方便、快捷,基本都使用string类,很少有人去使用C库中的字符串操作函数。实际上string类有100多种接口,但我们没办法全部实现出来,而且也有很多是几乎用不到的接口,本文主要介绍的是常用及常见的接口。
最后,感谢各位大佬的耐心阅读和支持,觉得本篇文章写的不错的朋友可以三连关注支持一波,如果有什么问题或者本文有错误的地方大家可以私信我,也可以在评论区留言讨论,再次感谢各位。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/564895.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

评奖系统设计

系列文章 任务40 评奖系统设计 文章目录 系列文章一、实践目的与要求1、目的2、要求 二、课题任务三、总体设计1.存储结构及数据类型定义2.程序结构3.所实现的功能函数4、程序流程图 四、小组成员及分工五、 测试执行完毕程序展示成功&#xff01;学生投票&#xff0c;举例第一…

URP的多Pass和Features用法

回到目录 大家好&#xff0c;我是阿赵。这里用一个传统的描边例子来说明一下&#xff0c;URP下怎么使用多Pass和Features。 一、传统多Pass描边 最常用的制作描边方法&#xff0c;就是写多一个Cull Front的Pass&#xff0c;然后通过法线方向扩展顶点&#xff0c;模拟描边的效…

机试打卡 -05 接雨水(动态规划栈)

我的思路&#xff1a;依次计算每一列能接收的雨水量。 关键点&#xff1a;如何计算得到每一列所能接收到的雨水量&#xff1f; 某一列能够接收到的雨水量&#xff0c;取决于其左右两侧最高的柱子。仅有当左右两侧的柱子均高于该列的高度&#xff0c;该列才可收到雨水&#x…

Java 17 VS Java 8: 新旧对决,这些Java 17新特性你不容错过

&#x1f3c5; 欢迎点赞 &#x1f44d; 收藏 ⭐留言 &#x1f4dd; 如有错误敬请指正&#xff01; Java是一门非常流行的编程语言&#xff0c;由于其跨平台性、可移植性以及强大的面向对象特性而备受青睐。Java最初由Sun Microsystems公司于1995年推出&#xff0c;随着时间的推…

[极客大挑战 2019]HardSQL1

拿到题目是一个登录界面 提交万能密码后拿到回显信息&#xff0c;说明页面存在过滤 burp抓包爆破后发现&#xff0c;所有736都是被过滤字符 联合注入和时间盲注被过滤&#xff0c;因为页面存在报错信息&#xff0c;所以尝试报错注入 因为空格也被过滤&#xff0c;所以我们使用括…

HOMER docker版本配置优化

概述 HOMER是一款100%开源的针对SIP/VOIP/RTC的抓包工具和监控工具。 HOMER是一款强大的、运营商级、可扩展的数据包和事件捕获系统&#xff0c;是基于HEP/EEP协议的VoIP/RTC监控应用程序&#xff0c;并可以使用即时搜索、处理和存储大量的信令、RTC事件、日志和统计信息。 …

机器学习-01概论

人们在生活中可能已经注意到了这样一种现象&#xff1a;我们能够轻松地通过相貌区分出日本人、韩国人和泰国人&#xff0c;但是面对英国人、俄罗斯人和德国人时&#xff0c;我们却很难辨认他们的面孔。造成这种现象的原因一方面是因为日韩泰都是我国的邻国&#xff0c;我们观察…

信号处理与分析-确定性信号的分析

目录 一、引言 二、确定性信号的定义 三、确定性信号的分类 四、确定性信号的分析方法 4.1 傅里叶变换 4.2 离散傅里叶变换 4.3 离散余弦变换 4.4 小波变换 五、确定性信号的处理方法 六、结论 一、引言 信号分析与处理是现代通信技术和信息处理技术的重要组成部分。…

Redis安装及其配置文件修改

一、redis 安装 点击即可下载 https://download.redis.io/releases/ 将下载后的包通过xftp上传到服务器 解压&#xff0c;我这边是解压到/usr/local目录下 -- 创建路径 mkdir /usr/local/redis -- 解压 tar -zxvf redis-4.0.0.tar.gz -C /usr/local/redis 为防止编译失败&am…

MyBatis-Plus精讲和使用注意事项

&#x1f353; 简介&#xff1a;java系列技术分享(&#x1f449;持续更新中…&#x1f525;) &#x1f353; 初衷:一起学习、一起进步、坚持不懈 &#x1f353; 如果文章内容有误与您的想法不一致,欢迎大家在评论区指正&#x1f64f; &#x1f353; 希望这篇文章对你有所帮助,欢…

【国产虚拟仪器】基于Zynq的雷达10Gbps高速PCIE数据采集卡方案(一)总体设计

2.1 引言 本课题是来源于雷达辐射源识别项目&#xff0c;需要对雷达辐射源中频信号进行采集传输 和存储。本章基于项目需求&#xff0c;介绍采集卡的总体设计方案。采集卡设计包括硬件设计 和软件设计。首先对采集卡的性能和指标进行分析&#xff0c;接着提出硬件的总体设计…

详解知识蒸馏原理和代码

目录 知识蒸馏原理概念技巧举例说明KL 散度及损失 KD训练代码导入包网络架构teacher网络student网络 teacher网络训练定义基本函数训练主函数 student网络训练&#xff08;重点&#xff09;理论部分定义kd的loss定义基本函数训练主函数 绘制结果teacher网络的暗知识softmax_t推…

使用dockerfile自定义Tomcat镜像

一&#xff1a;创建目录 mkdir /root/tomcat chmod 777 /root/ chmod 777 /root/tomcat 或者chmod -R 777 /root 这里的无效选项是因为我想递归修改root目录及root目录文件以下的权限 chmod :-R 递归修改指定目录下所有子目录和文件的权限 二&#xff1a;将jdk和apache压…

RPG游戏自动打怪之朝向判断

RPG游戏辅助想要做到自动打怪 获得到最近怪物信息以后 还需要面向怪物 否则背对怪物等等情况是没有办法攻击以及释放技能的 游戏设计的时候朝向是有很多种情况的 第一种 2D&#xff0c;2.5D老游戏&#xff0c;例如传奇 他的朝向一般是极为固定的4朝向或则8朝向 也就是不…

数组题目总结 -- 花式遍历

目录 一. 反转字符串中的单词思路和代码&#xff1a;I. 博主的做法II. 东哥的做法III. 其他做法1IV. 其他做法2 二. 旋转图像思路和代码&#xff1a;I. 博主的做法II. 东哥的做法 三. 旋转图像&#xff08;逆时针旋转90&#xff09;思路和代码&#xff1a;I. 博主和东哥的做法 …

SpringBoot2-基础入门(一)

SpringBoot2-基础入门&#xff08;一&#xff09; 文章目录 SpringBoot2-基础入门&#xff08;一&#xff09;1. 为什么学习SpringBoot1.1 SpringBoot的优点1.2 SpringBoot的缺点1.3 SpringBoot开发环境 2. 第一个SpringBoot程序2.1 添加依赖2.2 编写主程序类 -- 固定写法2.3 编…

SpringCloud(25):熔断降级实现

熔断降级会在调用链路中某个资源出现不稳定状态时&#xff08;例如调用超时或异常比例升高&#xff09;&#xff0c;对这个资源的调用进行限制&#xff0c;让请求快速失败&#xff0c;避免影响到其它的资源而导致级联错误。当资源被降级后&#xff0c;在接下来的降级时间窗口之…

硅谷新王登国会山,呼吁加强 AI 监管;马斯克任命推特新 CEO;数字媒体巨头申请破产;欧盟通过全球首个全面监管加密资产框架 | 经济学人第 21 周

1. 硅谷新王登国会山&#xff0c;呼吁加强 AI 监管 Sam Altman, the chief executive of OpenAI, the firm behind the ChatGPT chatbot, called for tighter regulation of rapidly developing generative artificial intelligence, such as by forcing disclosure on images …

【文件操作与IO】

目录 一、文件 1、文件的定义 2、File类 &#x1f345;File类中的常见属性 &#x1f345;File类中的构造方法 &#x1f345;File类中的常用方法 二、文件内容的读取-数据流 &#x1f345;InputStream概述 &#x1f345;FileInputStream &#x1f345;OutputStream 概…

真题详解(汇总)-软件设计(八十三)

真题详解&#xff08;include&#xff09;-软件设计&#xff08;八十二)https://blog.csdn.net/ke1ying/article/details/130828203 软件交付后进入维护阶段&#xff0c;采用专门的程序模块对文件或者数据中记录进行增加、删除和修改操作&#xff0c;属于&#xff1f; 解析&a…