[STL] string class (medium)

Table of contents

1, rbegin and rend

2,reserve & capacity

3,max_size ( )

4,size()& resize

1,void resize (size_t,char c)

5,push_back & append

1. Append string range

2,Direct addition


1, rbegin and rend

Details:cplusplus.com/reference/string/string/rbegin/

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

int main()
{
	string s1("hello world");

	string::reverse_iterator it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

In fact, it is to print the string in reverse;

And the shortcomings are also obvious. The type is too long and difficult to write;

So the usefulness of auto is reflected, it automatically helps us calculate the type;

int main()
{
	string s1("hello world");

	//string::reverse_iterator it = s1.rbegin();
	auto it = s1.rbegin();
	while (it != s1.rend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

This is better;

Another less popular one is to replace rengin with crengin and crend ​​with crend;

int main()
{
	string s1("hello world");

	//string::reverse_iterator it = s1.crbegin();
	auto it = s1.crbegin();
	while (it != s1.crend())
	{
		cout << *it << " ";
		it++;
	}

	return 0;
}

2,reserve & capacity

reserve: change the capacity, how much space is needed, just open it in advance

capacity: Returns the total size of the space

int main()
{
	string s1;
	string s2("hello world");

	//初始容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;
	cout << endl;

	s1.reserve(20);
	s2.reserve(30);

	//当 n>容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;
	cout << endl;

	s1.reserve(5);
	s2.reserve(5);

	//当 n<容量大小
	cout << s1.capacity() << endl;
	cout << s2.capacity() << endl;

	return 0;
}

Friends may be curious as to why the expanded capacity is not the same as the capacity we specified;

That's because the compiler has its own set of expansion mechanisms;

int main()
{
	string s1;
	string s2("hello world");

	int doll = s1.capacity();
	cout << s1.capacity() << endl;
	int i = 1000;
	while (i--)
	{
		s1 += ' ';
		if (doll != s1.capacity())
		{
			cout << s1.capacity() << endl;
			doll = s1.capacity();
		}
	}
	return 0;
}

Basically amplified by 1.5 times, except for the beginning;

Therefore, the space capacity will only be within these values. Even if it is 16, it will be directly expanded to the next stage 31. In fact, it is 32, because there is another ' \0 ';

When the expanded capacity is greater than the current capacity, expand;

When the expanded capacity is less than the current actual capacity, it remains unchanged, otherwise it is reduced;

The actual capacity of "hello world" in string s2("hello world") is 11;

3,max_size ( )

Calculate the maximum space that a string can open

int main()
{
	string s1;
	string s2("hello world");

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

	return 0;
}

It can be seen from the above that the maximum space that can be opened by the string class is the same;

But the maximum space that can be opened is not really openable. Let’s look at a piece of code;

int main()
{
	string s1;
	string s2("hello world");

	cout << s2.capacity() << endl;
	cout << s2.max_size() << endl;
	 
	s2.reserve(s2.max_size());
	cout << s2.capacity() << endl;

	return 0;
}

When the maximum space capacity is opened, the operation crashes directly and cannot be opened at all; 

The truth cannot be spoken, this is for reference only and does not need to be taken seriously;

4,size()& resize

size(): Returns the effective length of the string

resize(): Reduce the number of valid characters to n, and fill the extra space with the character c

Let’s look directly at a piece of code

int main()
{
	string s1;
	string s2("hello world");
	
	//打印有效字符长度和容量大小
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.resize(20);
	s2.resize(30);
	
	//有效字符对容量的影响
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.resize(5);
	s2.resize(8);
	//
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;
	cout << endl;

	s1.reserve(100);
	s2.reserve(200);

	//容量对有效字符的影响
	cout << s1.size() << " " << s1.capacity() << endl;
	cout << s2.size() << " " << s2.capacity() << endl;


	return 0;
}

It can be seen from the above that the length of the effective character size will affect the capacity capacity, but the size of the capacity capacity will not affect the effective character size;

Moreover, the effective character length of the string will change as size changes. Even if it is reduced, the capacity will not change. It can be used in the future to [delete data, keep the first n]

1,void resize (size_t,char c)

Extended tail plug

int main()
{
	string s1;
	string s2("hello world");

	s1.resize(10, 'y');
	s2.resize(20,'x');
	cout << s2 << endl;
	cout<< s1 << endl;

	return 0;
}

We can use this later to assign and initialize strings;

5,push_back & append

push_back: Insert character c at the end of the string

append: Append a string after the string

Go directly to the code:

int main()
{
	string s1;
	string s2("hello world");

	s1.push_back('x');
	s2.push_back('y');
	cout << s1 << endl;
	cout << s2 << endl;
	cout << endl;

	s2.append("hello world");
	s2.append("hello wprld");
	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

1. Append string range

string& append(inputiterator first,inputiterator last);

int main()
{
	string s1("abcdefg");
	string s2("hello world");

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

	string s3("abcdefg");
	s2.append(++s3.begin(), --s3.end());
	cout << s2 << endl;

	return 0;
}

Direct range is also possible, as well as ++, --;

int main()
{
	string s1;
	string s2("hello world");

	s1.append(s2,2,7);
	s2.append(10, 'x');

	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

 It is also possible to specify. There are many usages similar to those of the string class. You can check the various usages in the document;

2,Direct addition

int main()
{
	string s1;
	string s2("hello world");

	s1 += 'x';
	s2 += " abcdefg";
	cout << s1 << endl;
	cout << s2 << endl;

	return 0;
}

You can also add it directly, which is easier;

Guess you like

Origin blog.csdn.net/m0_71676870/article/details/134497006