Invincible STL (string management - string class)

1. Foreword

In C language, strings are represented by character arrays. For the application layer, strings are often used, and the continued use of character arrays makes the efficiency very low
Therefore, in the C + + standard library, the string is newly customized through the class string
When using the string class, you must include #include header files and using namespace std

2. Common interface description of string class

2.1 use of common constructors

Function nameFunction description
string()Constructor to construct an empty class
string(const char* s)Construct a string class object with a string
string(size_t n, char c)Construct an object with n characters c
string(const string&s)copy construction
int main()
{
	string s1;
	string s2("hello");
	string s3(10, 'c');
	string s4(s3);

	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;


	return 0;
}

2.2 capacity operation of string objects

Function nameFunction description
sizeReturns the valid character length of a string
capacityReturn space size
lengthReturns the valid character length of a string
clearClear valid characters
emptyCheck whether the string is empty
reserveReserve space for string
resizeChange the number of valid characters to n, and the extra space is filled with character c
	string s1("hello world");

	cout << s1.size() << endl;
	cout << s1.length() << endl;
	cout << s1.capacity() << endl;

	string s1("hello world XXXXXXXXXXXXXXXXXXX");

	cout << s1 << endl;
	cout << s1.capacity() << endl;
	cout << s1.size() << endl;
	s1.clear();
	
	cout << "-------" << endl;

	cout << s1.capacity() << endl;
	cout << s1.size() << endl;
	cout << s1 << endl;

Clear the string in s1. Note that when clearing, only clear the size to 0 without changing the size of the underlying space

	string s1("hello world");

	// Increase the number of valid characters in s1 to 10, and fill the extra positions with 'a'
	s1.resize(20, 'a');
	cout << s1 << endl;
	cout << s1.size() << endl;
	cout << s1.capacity() << endl;
	string s("hello world");
	// Increase the number of valid characters in s to 15, and fill the extra positions with the default value '\ 0'
	// "aaaaaaaaaa\0\0\0\0\0"
	// Note that the number of valid characters in s has increased to 15
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

Both resize(size_t n) and resize(size_t n, char c) change the number of valid characters in the string to N. the difference is that when the number of characters increases: resize(n) fills the extra element space with 0, and resize(size_t n, char c) fills the extra element space with character C. Note: when resizing the number of elements, increasing the number of elements may change the size of the underlying capacity. If reducing the number of elements, the total size of the underlying space will remain unchanged.

reserve(size_t res_arg=0): reserve space for string without changing the number of valid elements. When the reserve parameter is less than
When the total size of the underlying space of string, the reserver will not change the capacity.

2.3 modifying string objects

Function nameFunction description
push_backTrailing character
appendTrailing string
operator+=Trailing string
c_strReturns a C format string
findFind the character c from the pos position of the string and return the position of the character in the string
substrIn str, n characters are intercepted from the pos position, and then returned

3. Implement a common string

class string
{
public:
	string(const char* str = "")
	{
		// When constructing a string class object, if the nullptr pointer is passed, it is considered that the program is illegal. Assert here
		
		if (nullptr == str)
		{
			assert(false);
			return;
		}

		_str = new char[strlen(str) + 1];
		strcpy(_str, str);
	}

	string(const string& s)
		: _str(new char[strlen(s._str) + 1])
	{
		strcpy(_str, s._str);
	}

	string& operator=(const string& s)
	{
		if (this != &s)
		{
			char* pStr = new char[strlen(s._str) + 1];
			strcpy(pStr, s._str);
			delete[] _str;
			_str = pStr;
		}

		return *this;
	}

	~string()
	{
		if (_str)
		{
			delete[] _str;
			_str = nullptr;
		}
	}

private:
	char* _str;
};

Tags: C++

Posted on Sun, 12 Sep 2021 03:38:11 -0400 by konetch