string类中的一些问题

    前言:C++中的string类是继承C语言的字符数组的字符串来实现的,其中包含许多C的字符串的相关知识的同时,也蕴含很多的类与对象的相关知识,在面试中,面试官总喜欢让学生自己来模拟实现string类,最主要是实现string类的构造、拷贝构造、赋值运算符重载以及析构函数。今天,我们就来简单分析和实现一下string类的相关的功能,那么,朋友,来吧~~~

 为了方便后序阅读,这是我自定义的string类的数据成员和部分主要函数:

namespace my_std
{
	class string
	{
	public:

		/*string()
			:_str(new char[1]{'\0'})
			,_size(0)
			,_capacity(0)
		{}*/

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
        ~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		size_t capacity() const
		{
			return _capacity;
		}

		size_t size() const
		{
			return _size;
		}

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

		private:
		char* _str;//指向由new开辟出来的动态空间,该空间存储字符串
		size_t _size;//string对象对应字符串的大小
		size_t _capacity;//string对象的容量,在存储的时候会多存储一个空字符,方便转化为C风格的字符串,在不同的环境扩容机制有些许不同,有的直接2倍,有的1.5倍

		//const static size_t npos = -1;  // 
		//const static double npos = 1.1;  // ֧
	 public:
		const static size_t npos;//静态变量类内声明,类外初始化
};
const size_t string::npos = -1;
        

目录

1. 深拷贝与浅拷贝

拷贝构造函数

函数的传值返回与赋值操作

2.iterator与const_iterator

范围for与迭代器

3.类型不匹配问题带来的错误  

类型提升

4.输入流运算符重载问题

5.源码在这

string.h

test.cpp


1. 深拷贝与浅拷贝

      浅拷贝:也称位拷贝,编译器只是将对象中的值拷贝过来。如果对象中管理资源,最后就会导致多个对象共 享同一份资源,当一个对象销毁时就会将该资源释放掉,而此时另一些对象不知道该资源已经被释放,以为 还有效,所以当继续对资源进项操作时,就会发生发生了访问违规。

      深拷贝:如果一个类中涉及到资源的管理,也就是其数据成员保存在另一块开辟好的空间里而并直接在对象所代表的数据成员本身上(比如指针等数据成员一般都会涉及深浅拷贝问题),其拷贝构造函数、赋值运算符重载以及析构函数必须要显式给出。一般情况都是按照深拷贝方式提供。

而在string类中经常出问题的一般在以下两个方面:

拷贝构造函数

      使用一个string对象来初始化另一个string对象时,可能会触发拷贝构造函数,导致字符串数据的拷贝,如果采用浅拷贝,就相当于把两个对象在堆上开辟的空间绑到了一起,两个任意一个都可以对该空间进行修改和操作,从而导致出错。

     所以,我们需要进行深拷贝,也就是,将其指向在堆上的空间在堆上的另一个地方复制一份给另一个对象,就像原来我和你共用100元,那我的花销肯定要受你影响,但是现在我们一人100元,这样虽然我们都是100元,但是两个人用钱却互不耽误,并且两张钱的编号也不一样,这样两个对象在堆上的数据成员存储的内容虽然是一样的,但是其在堆上的空间却是两个不一样的空间,从而使两个对象各玩各的,互不影响。

//传统的拷贝构造
		string(const string& str)//拷贝构造(深拷贝)防止传值返回的对象重复析构问题
		{
			_str = new char[str._capacity + 1];
			strcpy(_str, str._str);
			_size = str._size;
			_capacity = str._capacity;

		}
//拷贝构造的现代写法
		void swap(string& str)
		{
			std::swap(str._str, _str);
			std::swap(_size, str._size);
			std::swap(_capacity, str._capacity);
		}
		string(const string& str)
			:_str(nullptr)
			,_size(0)
			,_capacity(0)
		{
			string tmp(str._str);//临时对象出作用域自动调用析构
			swap(tmp);
		}

函数的传值返回与赋值操作

     我们都知道,临时对象具有常性,且临时对象作为函数的传值返回时,其实质是将临时对象拷贝一份,并且将原临时对象销毁(调用析构函数),并返回拷贝的对象,当然,我们上们已经给出了拷贝构造函数,这就让我们在拷贝临时对象的时候采用深拷贝,产生的拷贝对象不会和原临时对象再共用堆上的空间而是自己开辟新空间,这样一来,原临时对象的析构就不会影响拷贝对象在函数结束后的进一步操作。

string substr(size_t pos, size_t len = npos)
		{
			string s;
			size_t end = pos + len;
			if (len == npos || pos + len >= _size)
			{
				len = _size - pos;
				end = _size;
			}
			s.reserve(len);
			for (size_t i = pos; i < end; i++)
				s += _str[i];
			return s;
		}

sub1 = s3.substr(0, i1);//赋值运算符带来的浅拷贝问题

       同样的,赋值操作如果不出来也会带来一定的问题,比如,我的两个字符串原来都存有内容,如果调用赋值运算符将一个对象中存储的内容变成另一个对象中的内容,简单的=并不能对堆上的内容进行统一,因为这还涉及字符串长度,空间问题,所以也需要进行深拷贝。

//解决类似s2=s3的赋值问题(异地扩容)
		string& operator=(const string& str)
		{
			if (this != &str)
			{
				//C++中,只有空间相近才会原地扩容,否则一般都为异地扩容,所以我们直接异地开空间
				char* temp = new char[str._capacity + 1];//类内部可以访问任意的属于该类的对象的私有成员
				strcpy(temp, str._str);
				delete[]_str;
				_str = temp;
				_size = str._size;
				_capacity = str._capacity;
			}
			return *this;
		}

2.iterator与const_iterator

      迭代器(Iterator)是一种用于遍历序列容器中元素的对象,作为容器的一种抽象,提供访问和操作容器元素的方法。迭代器通常包含指向容器中元素的指针或引用,并提供遍历序列的功能(如递增、递减、解引用等)。

      使用迭代器可以手动控制序列的遍历过程,对容器进行精细的遍历和操作。在传统的for循环中,常用的方法是使用迭代器来遍历容器,通过迭代器的自增操作来依次访问容器中的元素。

范围for与迭代器

      范围for循环可以看作是迭代器的一种简化和自动化形式,更适合处理简单的容器遍历操作。它会自动根据容器的类型选择遍历的方式,无需显式地使用迭代器。但对于复杂的遍历过程或对容器进行更精细的操作,还是需要使用迭代器来实现。

//迭代器的实现       
        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;
		}
//应用
ostream& operator<<(ostream& out, const string& s)
	{
		/*for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}*/
		for (auto ch : s)//因为是const对象,所以需要调用const类型的迭代器
			out << ch;

		return out;
	}
void test_string1()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;

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

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

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			(*it)++;//能修改原字符串
			cout << *it << " ";
			++it;
		}
		cout << endl;
		cout <<s1.c_str()<< endl;

		for (auto& ch : s1)//加引用可以修改原字符串
		{
			ch--;
			cout << ch << " ";
		}
		cout << endl;

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

3.类型不匹配问题带来的错误  

   在string中,我们采用了无符号整形类型size_t,其类型和int想比还是有很大的区别的:

  1. 数据范围: size_t是一个无符号整数类型,可以表示非负数。它的范围通常是整个非负整数范围,即0到2^(sizeof(size_t)*8)-1。而int是一个有符号整数类型,可以表示正数、负数和零。其范围通常是-2^(sizeof(int)*8-1)到2^(sizeof(int)*8-1)-1。

  2. 符号位: size_t是无符号整数类型,没有符号位,所有的比特位用于表示数值。而int是有符号整数类型,其中最高位被用作符号位,用于表示正负性。

  3. 运算结果: 对于int,整数运算遵循有符号数的规则,包括加法、减法、乘法和除法等。当运算结果溢出时,将发生有符号数的溢出行为。而size_t是无符号整数类型,溢出时会进行模运算,即结果会被截断为0到最大值的范围。

      在混合使用size_tint类型时,要小心处理类型转换和溢出的情况,以避免意外的错误。比如下面的情况:

void insert(size_t pos, const char* str)//向字符串的任意位置插入一个字符串
		{
			assert(pos <= _size);//等于相当于尾插
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;//指向'\0'的位置上,在挪动数据时可以直接将'\0'挪动到字符串末尾
			while (end >= (int)pos)//强转,防止size_t和int的类型不兼容而发生像-1>=0的情况
			{
				_str[end + len] = _str[end];
				end--;
			}
			strncpy(_str + pos, str, len);//不能将str的'\0'也拷贝进去
			_size += len;
		}

类型提升

<1>、C语言中的整型提升规则编辑
如果运算的操作数是同一类型则不进行类型提升,如果操作数之间的类型不一致且都为整型,
(1)当操作数都为无符号整型或者都为无符号整型时,较短的操作数将转换为较长的操作数再进行运算(即sizeof的值较小的向sizeof较大的转换),
(2)当操作数中既有有符号整型又有无符号整型时,
     a、若有符号整型的操作数的长度小于等于无符号整型的操作数,那么其将转换为无符号整型的操作数,    
     b、若有符号整型的操作数的长度大于无符号整型的操作数,那么有符号整型转换为无符号整型参与运算后的结果,再转换为有符号整型。
<2>、C语言中的浮点类型提升规则编辑
(1)当操作数中最长的一个类型为long double类型,则其他操作数类型提升至long double类型再进行运算,
(2)当最长类型为double,则其他操作数提升至double类型,
(3)当最长类型为float,则其他操作数提升至float类型,
在这个过程中整型操作数全部转换为对应操作数的浮点类型,浮点数进行运算时的结果可能会用范围与精度更大的浮点类型表示。
 

这里我们提供两种解决办法,一种是避开类型提升,一种是直接强转禁止类型提升:

// insert(0, 'x')
		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)//向字符串的任意位置插入一个字符串
		{
			assert(pos <= _size);//等于相当于尾插
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;//指向'\0'的位置上,在挪动数据时可以直接将'\0'挪动到字符串末尾
			while (end >= (int)pos)//强转,防止size_t和int的类型不兼容而发生像-1>=0的情况
			{
				_str[end + len] = _str[end];
				end--;
			}
			strncpy(_str + pos, str, len);//不能将str的'\0'也拷贝进去
			_size += len;
		}

4.输入流运算符重载问题

     对于自定义类型的类对象进行输入的时候,一般都需要自定义输入流来获取特定规则的数据,对于字符串也是一样,读取一个字符串,我们需要考虑读入字符的数量来决定字符串初始化预开设的空间,但是这个空间我们并不能事先知道准确值,所以我们采用一定的方法来使得数组可以根据需要开辟与字符串长度相近的空间大小。

istream& operator>>(istream& in, string& s)
	{
		s.clear();//首先把原来的数据清掉
		char buff[129];//存储128个字符,最后一个字符需要存'\0'
		size_t i=0;
		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 128)
			{
				buff[i] = '\0';
				s+=buff;
				i = 0;
			}
			//in >> ch;这种方式拿不到空格或者换行符,因为C++将这些字符当做分割符,自动跳过
			ch = in.get();
		}
		if (i != 0)//说明最后还有不到128个字符
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}

5.源码在这

string.h

#pragma once
#include<assert.h>

namespace my_std
{
	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()
			:_str(new char[1]{'\0'})
			,_size(0)
			,_capacity(0)
		{}*/

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}
		//传统的拷贝构造
		string(const string& str)//拷贝构造(深拷贝)防止传值返回的对象重复析构问题
		{
			_str = new char[str._capacity + 1];
			strcpy(_str, str._str);
			_size = str._size;
			_capacity = str._capacity;

		}
		拷贝构造的现代写法
		//void swap(string& str)
		//{
		//	std::swap(str._str, _str);
		//	std::swap(_size, str._size);
		//	std::swap(_capacity, str._capacity);
		//}
		//string(const string& str)
		//	:_str(nullptr)
		//	,_size(0)
		//	,_capacity(0)
		//{
		//	string tmp(str._str);//临时对象出作用域自动调用析构
		//	swap(tmp);
		//}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

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

			return _str[pos];
		}

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

			return _str[pos];
		}

		size_t capacity() const
		{
			return _capacity;
		}

		size_t size() const
		{
			return _size;
		}

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

		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';
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}

			strcpy(_str + _size, str);
			_size += len;
		}

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

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

		// insert(0, 'x')
		void insert(size_t pos, char ch)
		{
			assert(pos <= _size);
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}

			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			_size++;
		}

		void insert(size_t pos, const char* str)//向字符串的任意位置插入一个字符串
		{
			assert(pos <= _size);//等于相当于尾插
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			int end = _size;//指向'\0'的位置上,在挪动数据时可以直接将'\0'挪动到字符串末尾
			while (end >= (int)pos)//强转,防止size_t和int的类型不兼容而发生像-1>=0的情况
			{
				_str[end + len] = _str[end];
				end--;
			}
			strncpy(_str + pos, str, len);//不能将str的'\0'也拷贝进去
			_size += len;
		}

		void erase(size_t pos, size_t len = npos)
		{
			assert(pos < _size);
			if (len == npos || pos + len >= _size)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				size_t begin = pos + len;
				while (begin <= _size)
				{
					_str[begin-len] = _str[begin];
					begin++;
				}
				_size -= len;
			}
		}
		void resize(size_t n, char ch = '\0')
		{
			if (n <= _size)
			{
				_str[n] = '\0';
				_size = n;
			}
			else
			{
				reserve(n);
				while (_size < n)
				{
					_str[_size] = ch;
					_size++;
				}
				_str[_size] = '\0';
			}
		}
		size_t find(char ch, size_t pos = 0)
		{
			for (size_t i = pos; i < _size; i++)
			{
				if (_str[i] == ch)
				{
					return i;
				}
			}

			return npos;
		}
		string substr(size_t pos, size_t len = npos)
		{
			string s;
			size_t end = pos + len;
			if (len == npos || pos + len >= _size)
			{
				len = _size - pos;
				end = _size;
			}
			s.reserve(len);
			for (size_t i = pos; i < end; i++)
				s += _str[i];
			return s;
		}
		size_t find(const char* sub, size_t pos = 0)
		{
			const char* p = strstr(_str+pos, sub);
			if (p)
				return p - _str;
			else
				return npos;
		}
		
		bool operator<(const string& s) const
		{
			return strcmp(_str, s._str) < 0;
		}

		bool operator==(const string& s) const
		{
			return strcmp(_str, s._str) == 0;
		}

		bool operator<=(const string& s) const
		{
			return *this < s || *this == s;
		}

		bool operator>(const string& s) const
		{
			return !(*this <= s);
		}

		bool operator>=(const string& s) const
		{
			return !(*this < s);
		}

		bool operator!=(const string& s) const
		{
			return !(*this == s);
		}
		//解决类似s2=s3的赋值问题(异地扩容)
		string& operator=(const string& str)
		{
			if (this != &str)
			{
				//C++中,只有空间相近才会原地扩容,否则一般都为异地扩容,所以我们直接异地开空间
				char* temp = new char[str._capacity + 1];//类内部可以访问任意的属于该类的对象的私有成员
				strcpy(temp, str._str);
				delete[]_str;
				_str = temp;
				_size = str._size;
				_capacity = str._capacity;
			}
			return *this;
		}
		
		void clear()
		{
			_str[0] = '\0';
			_size = 0;
		}
	private:
		char* _str;//指向由new开辟出来的动态空间,该空间存储字符串
		size_t _size;//string对象对应字符串的大小
		size_t _capacity;//string对象的容量,在存储的时候会多存储一个空字符,方便转化为C风格的字符串,在不同的环境扩容机制有些许不同,有的直接2倍,有的1.5倍

		//const static size_t npos = -1;  // 
		//const static double npos = 1.1;  // ֧
	 public:
		const static size_t npos;//静态变量类内声明,类外初始化
};

	const size_t string::npos = -1;

	ostream& operator<<(ostream& out, const string& s)
	{
		/*for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}*/
		for (auto ch : s)//因为是const对象,所以需要调用const类型的迭代器
			out << ch;

		return out;
	}

	istream& operator>>(istream& in, string& s)
	{
		s.clear();//首先把原来的数据清掉
		char buff[129];//存储128个字符,最后一个字符需要存'\0'
		size_t i=0;
		char ch;
		ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			if (i == 128)
			{
				buff[i] = '\0';
				s+=buff;
				i = 0;
			}
			//in >> ch;这种方式拿不到空格或者换行符,因为C++将这些字符当做分割符,自动跳过
			ch = in.get();
		}
		if (i != 0)//说明最后还有不到128个字符
		{
			buff[i] = '\0';
			s += buff;
		}
		return in;
	}

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

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

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

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			(*it)++;//能修改原字符串
			cout << *it << " ";
			++it;
		}
		cout << endl;
		cout <<s1.c_str()<< endl;

		for (auto& ch : s1)//加引用可以修改原字符串
		{
			ch--;
			cout << ch << " ";
		}
		cout << endl;

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

	void test_string2()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		s1.push_back(' ');
		s1.append("hello bit hello bit");

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

		s1 += '#';
		s1 += "*********************";
		cout << s1.c_str() << endl;

		string s2;
		s2 += '#';
		s2 += "*********************";
		cout << s2.c_str() << endl;
	}

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

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

		s1.insert(s1.size(), '%');
		cout << s1.c_str() << endl;

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

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

		cout << (s1 >= s2) << endl;

		s1[0] = 'z';
		cout << (s1 >= s2) << endl;

		cout << s1 << endl;
		cin >> s1;
		cout << s1 << endl;

		/*char ch1, ch2;
		cin >> ch1 >> ch2;*/
	}
	void test_string5()
	{
		string s1("hello world");
		s1.insert(5, "abc");
		cout << s1 << endl;

		s1.insert(0, "xxx");
		cout << s1 << endl;

		s1.erase(0, 3);
		cout << s1 << endl;

		s1.erase(5, 100);
		cout << s1 << endl;

		s1.erase(2);
		cout << s1 << endl;
	}

	void test_string6()
	{
		string s1("hello world");
		cout << s1 << endl;

		s1.resize(5);
		cout << s1 << endl;

		s1.resize(25, 'x');
		cout << s1 << endl;
	}

	void test_string7()
	{
		string s1("test.cpp.tar.zip");
		//size_t i = s1.find('.');
		//size_t i = s1.rfind('.');

		//string s2 = s1.substr(i);
		//cout << s2 << endl;

		string s3("https://legacy.cplusplus.com/reference/string/string/rfind/");
		// 协议
		// 域名
		// 资源名

		string sub1, sub2, sub3;
		size_t i1 = s3.find(':');
		if (i1 != string::npos)
			sub1 = s3.substr(0, i1);//substr函数为值返回,会有对象的析构问题,返回的对象实际上是原临时对象的拷贝,我们要让原本的浅拷贝变为深拷贝,同时也要对赋值运算符进行重载,重载为深拷贝形式

		else
			cout << "没有找到\":\"" << endl;

		size_t i2 = s3.find('/', i1 + 3);
		if (i2 != string::npos)
			sub2 = s3.substr(i1 + 3, i2 - (i1 + 3));
		else
			cout << "没有找到\"/\"" << endl;

		sub3 = s3.substr(i2 + 1);

		cout << sub1 << endl;
		cout << sub2 << endl;
		cout << sub3 << endl;
	}

	void test_string8()
	{
		string s1("hello world");
		string s2 = s1;
		cout << s1 << endl;
		cout << s2 << endl;

		string s3("xxxxxxxxxxxxxxxxxxx");
		s2 = s3;

		cout << s2 << endl;
		cout << s3 << endl;
	}

	void test_string9()
	{
		string s1("hello world");
		cin >> s1;
		cout << s1 << endl;
		cout << s1.size() << endl;
		cout << s1.capacity() << endl;
	}
}

test.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>

using namespace std;//命名空间先展开,方便自定义的string类中使用像cout之类的输出符号
#include "string.h"


int main()
{
	//my_std::test_string1();
	//my_std::test_string2();
	//my_std::test_string3();
	my_std::test_string4();
	//my_std::test_string5();
	//my_std::test_string8();//拷贝构造和赋值
	//my_std::test_string7();



	//对于库里的string
	//string s1;
	//cout << s1.capacity() << endl;

	//string s2("Hello world!");
	//cout << s2.capacity() << endl;
	开始时初始化一个buff数组,所有的对象在字符串不超过buff数组容量的时候不会在堆上再申请空间,而是用已经开好的buff数组存储,这也就是string对象的字符串长度在一定限度内的容量空间是一样的原因
	//string s3("Hello world!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
	//cout << s3.capacity() << endl;

	//cout << sizeof(s1) << endl;
	//cout << sizeof(s2) << endl;
	//cout << sizeof(s3) << endl;

	return 0;
}

       

   最好的状态就是清楚自己要什么,并为之全力以赴。能把你从低谷拔出来的从来都不是别人,而是你自己真正想要改变的信念,短时间的消沉、抱怨并不可怕,努力向前走吧,勇敢一点吧,失败跟遗憾相比简直不值一提。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值