[STL] One-stop strategy for using string class

Table of contents

One, STL

1 Introduction

2. The version of STL

3. Six components of STL  

4. Learning STL, three realms

5. Learn to view C++ documentation 

Second, the string class

1. Compared with C language, why do we need to learn C++ string?

2. Header file

3. Common constructors

4.  operator=   

5. operator[] && at function

6. String capacity

1. About the choice of size and length

2. About the expansion mechanism of the string class

3. Set capacity

7. iterators——iterator (important)

1. Looking back at the scope of C++ entry for (syntactic sugar for)

2. Reverse iteration

8. String insertion

1. Common insertion methods:

2. About the use of insert in the middle 

9. Delete string - erase

10.  c_str

1. Usage scenarios

 2. The difference between '\0' in C language & C++string class

11. find & rfind

1. Get the file name suffix

 2. Separate URLs

12. getline

13. Convert strings to other data (C++11)

Summary of the use of the string class

epilogue


One, STL

1 Introduction

STL (standard template libaray-standard template library): It is an important part of the C++ standard library . It is not only a reusable component library, but also a software framework including data structures and algorithms .

2. The version of STL

Original copy
The original version completed by Alexander Stepanov and Meng Lee in Hewlett-Packard Labs, in the spirit of open source, they declare that they allow anyone to freely use, copy, modify, disseminate, and commercially use these codes without payment. The only condition is that it also needs to be used as open source as the original version. The HP version -- the granddaddy of all STL implementations.
PJ version
Developed by PJ Plauger , inherited from HP version, adopted by Windows Visual C++ , cannot be disclosed or modified, defect: relatively low readability,
Symbol naming is a bit weird.
RW version
Developed by Rouge Wage Company, inherited from HP version, adopted by C++ Builder , cannot be disclosed or modified, and the readability is average.
SGI version
Developed by Silicon Graphics Computer Systems , Inc , inherited from the HP version. Adopted by GCC (Linux) , it has good portability and can be disclosed, modified or even sold. From the perspective of naming style and programming style, it is very readable. When we learn STL later, we need to read part of the source code, and the main reference is this version .

3. Six components of STL  

4. Learning STL, three realms

1. Familiar with using STL.

2. Be able to clearly understand the underlying principles of STL.

3. Ability to write extensions to STL. (During the school period, a few can master it)

5. Learn to view C++ documentation 

  Reference - C++ Reference (cplusplus.com)

There are about 100 interfaces in the string class alone in the study, we are proficient in 20 common ones, and the remaining 80 or so require us to search for documents, and the best way is to view the original English documents. 

Second, the string class

1. Compared with C language, why do we need to learn C++ string?

In C language, a string is a collection of some characters ending with '\0'. For the convenience of operation, the C standard library provides some library functions of the str series, but these library functions are separated from the string. It conforms to the idea of ​​OOP, and the underlying space needs to be managed by the user, and there may be cross-border .

2. Header file

 When using the string class, you must include the header file and using namespace std ;

#include <string>

3. Common constructors

 Common string class constructors and functions:

1. string() (emphasis)                              constructs an empty string class object, that is, an empty string
2. string(const char* s) (emphasis)       use C-string to construct string class object
3. string(size_t n, char c) string class object contains n characters c
4. string(const string&s) (emphasis)  copy constructor   

Here is the original text:

Select constructor (function) to enter: 

visible 

Suggestion: Try to read the original document, explaining the function function.

At the same time, from viewing the document, we can see that the string class overloads  stream extraction & stream insertion, we directly cout, cin

The demonstration is as follows:

int main()
{
	// string(const char* s) (重点)  用string来构造string类对象
	string s1("hello, C++");
	cout << "s1 :" << s1 << endl;

	// string() (重点)  构造空的string类对象,即空字符串
	string s2;
	cout << "s2 :" << s2;
	cin >> s2;
	cout << "s2 :" << s2 << endl;

	// string(const string & s) (重点) 拷贝构造函数
	string s3(s1);
	cout << "s3 :" << s3 << endl;

	// string(size_t n, char c)   string类对象中包含n个字符c
	string s4(4, 'c');
	cout << "s4 :" << s4 << endl;
	
	return 0;
}

Here is a separate explanation of the third function,  the subchain function:

4.  operator=   

 Member function:

 function display:

int main()
{
	string s2;
	// string & operator= (const char* s);
	string s1 = "hello, C++"; // 构造 + 拷贝 -> 优化为一次构造
	cout << "s1: " << s1 << endl; // 结果: hello, C++

	// string& operator= (const string & str); 
	s2 = s1;
	cout << "s2: " << s2 << endl; // 结果: hello, C++
	
	// string& operator= (char c);
	s2 = 'x';
	cout << "s2: " << s2 << endl; // 结果: x

	return 0;
}

Supplement: The assign function is similar to assignment. If you are interested, you can check the documentation, and I will not list it here.

5. operator[] && at function

Function description : Both are access strings that make the string class like an array.

Member function:

 

The difference is in the return value 

An assertion  will occur when operator[] is out of bounds ; an exception .

function display: 

int main()
{   
	// 1. char& operator[] (size_t pos); // 返回可修改的别名
	string s1 = "hello, C++";
	cout << s1[4] << endl;  // at :  s1.at(4)
	s1[4] = 'k';  // 如果类对象实例化时,未被const修饰,同时也具有修改的能力

	// 2. const char& operator[] (size_t pos) const;
	const string s2 = s1;
	cout << s2[4] << endl;
	s2[4] = 'o'; // s2 被const修饰,不允许修改其内容

	return 0;
}

 6. String capacity

1. About the choice of size and length

Both functions are to get the length of the string in the container

 It is generally recommended to use size() , because length debuted earlier, size debuted later, and size is often used in the string class. (Note: max_size is meaningless, all returns are 4.1 billion)

Simple traversal to output a string:

int main()
{   
	string s1 = "hello, C++";
	for (int i = 0; i < s1.size() ; i++)
	{
		cout << s1[i];  // at: s1.at(i);
	}
	return 0;
}

2. About the expansion mechanism of the string class

Conclusion: The expansion mechanism may be different under different compilers, some are 1.5 times, and some are 2 times expansion. (The STL standard library is just a standard, and different compiler vendors have their own function implementation methods)

Here is the result of expanding capacity under linux g++ and VS2019 with the same piece of code:

 

3. Set capacity

We know that frequent capacity expansion will cause additional consumption, so we pre-set a certain size of capacity to reduce the number of capacity expansions.

function:

Simple to use:

    s1.reserve(1000); // 单纯开空间

	s2.resize(1000);  // 开空间, 同时设置字符,默认为‘0’
	s2.resize(1000, 'c');

7. iterators——iterator (important)

The iterator is an internal class type in string, and its function is similar to operator[], accessing data like an array. But the bottom layer of the string is an array, which is a continuous space, and it is more convenient to use operator[]. After we learn STL's vector (sequential table) and list (linked list) later, we will find that operator[] is not bad for continuous storage data structure access, but linked lists, trees, and iterators are more suitable, and their usage is similar, so We also learn about iterators. ( The iterator is a common access method for containers, the usage is similar, and it is on the road ahead of time )

Simple usage:

int main()
{
   string s1 = "hello, C++";
	string::iterator it = s1.begin();
	while (it != s1.end()) // != 原因:1. string类本身有关 2.其他容器也是用 != 这样设计统一了 
	{
		cout << *it;
		it++;
	}
	return 0;
}

s1.begin() and s1.end() return the position of the iterator is [ left closed interval right open interval)  

1. Looking back at the scope of C++ entry for (syntactic sugar for)

 int main()
{   
    string s1 = "hello, C++";
	for ( auto e : s1) // 自动++, 自动判断结束
	{
		cout << e;
	}
return 0;
}

The bottom layer of syntactic sugar is implemented through iterators and can only be traversed forward.

2. Reverse iteration

Function: reverse traversal data

 Simple usage:

int main()
{
    string s1 = "hello, C++";
	string::reverse_iterator it = s1.rbegin(); 
	while ( it != s1.rend())
	{
		cout << *it;
		it++; // 底层肯定是反向++
	}
  return 0;
}

Of course, whether it is s1.begin, s1.end; s1.rbegin, s2.rend, there are overloaded iterator functions that return const decoration (usually occurs when passing string references, such as: const string& s1)

8. String insertion

In the C language, the library function strcat also has such a function, but it is not as smart as C++, and the disadvantages of the C language are:

  • 1. Every time a character is inserted, the string needs to traverse the entire string, which is inefficient , and it is necessary to ensure that the inserted character array is large enough .
  • 2. C++ insertion will record the subscript of the last character, which can be inserted directly.

1. Common insertion methods:

1. push_back

2. append

3. operator += 

Check the document in detail

Short answer operation:

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string s1("hello, C++");
	s1.push_back('6');
	s1.push_back('-');
	s1.push_back('-');
	cout << s1 << endl;

	s1.append("5");
	cout << s1 << endl;

	string s2(s1);
	s2 += s1;
	s2 += 'c';
	s2 += "你好";
	cout << s2 << endl;
	return 0;
}

2. About the use of insert in the middle 

Conclusion: Use as little as possible , string class, inserting in the middle will cause the following characters to shift. Suppose you want to insert n characters at the beginning, the second time, move once; the third time, move 2 times; the nth time, move n - 1 time; so the time complexity of shifting is n2 , so use it as little as possible.

Common use of three inserts:

Use as follows:

int main()
{
	// 1. 迭代器
	string s1 = "hello, C++";
	string::iterator it = s1.begin() + 2; // 也就是在下标为2的位置插入
	s1.insert(it, 'Z');
	cout << s1 << endl; // heZllo, C++


	string s2 = s1;
	s2.insert(2, "你好"); // 下标为2的位置
	cout << s2 << endl;

	string s3 = s1;
	s3.insert(5, s2);    // 下标为5的位置插入
	cout << s3 << endl;
	return 0;
}

9. Delete string - erase

 Function: erase

Code practice: 

int main()
{
	// 1. 迭代器
	string s1 = "hello, C++";
	string::iterator it = s1.begin() + 2; // 也就是在下标为2的位置插入
	s1.insert(it, 'Z');
	cout << s1 << endl; // heZllo, C++


	string s2 = s1;
	s2.insert(2, "你好"); // 下标为2的位置
	cout << s2 << endl;

	string s3 = s1;
	s3.insert(5, s2);    // 下标为5的位置插入
	cout << s3 << endl;

	// 1. 迭代器删除
	//s3.erase(2);
	string::iterator it2 = s3.begin() + 2;
	string::iterator it3 = s3.end();
	s3.erase(it2, it3);
	cout << s3 << endl;
	return 0;
}

10.  c_str

1. Usage scenarios

When the C language interface is needed, the C language does not support the C++ string class, and the c_str member function can convert the string class string into a C language compatible string .

Such as: access folders in C language

    string s1("Test.cpp");
	const char* file = s1.c_str();
	FILE* myfile = fopen( file, "r"); // 不认string类
	char i = fgetc(myfile);
	while ( i != EOF)
	{
		cout << i;
		i = fgetc(myfile);
	}

 2. The difference between '\0' in C language & C++string class

in conclusion: 

C language is more important : '\0' In C language, most string interface functions such as strcopy, strcat end with '\0' as the end of the string ;

C++ directly ignores: We can set any position in the middle of the string to \0, but in C++, the string will treat \0 as a normal character ( it will be output as a space or a character that does not occupy a position when printing )

int main()
{
	string s1("test.cpp");
	s1 += '\0';
	s1 += '\0';
	s1 += '\0';
	s1 += '\0';
	s1 += "test.cpp";
	cout << s1.c_str() << endl; // ‘\0’对于C语言,是字符串结束的标志
	cout << s1 << endl;         //   而在string中,字符串中可以存在,但不发生显示或者是空格。
	
	return 0;
}

11. find & rfind

pos: the character position to start searching

n: the length of the matched character sequence

1. Get the file name suffix

int main()
{   // 1. 简单文件获取后缀
	string s1("test.cpp");
	size_t i = s1.find('.');
	string tmp;
	if (i != string::npos)
	{
		tmp = s1.substr(i); // 先用获取子串函数
	}
	cout << tmp << endl;

	// 2. 复杂后缀,而只取最后的后缀
	string s2("test.cpp.tar.zip");
	size_t i2 = s2.rfind('.');  // 从后向前寻找
	string tmp2;
	if (i2 != string::npos)
	{
		tmp2 = s2.substr(i2);
	}
	cout << tmp2;
	return 0;
}

 Add substr:

 2. Separate URLs

int main()
{
	string s1("https://mp.csdn.net/mp_blog/creation/editor/131103529");
	// 1. 协议
	size_t pl = s1.find("://");
	size_t next = pl;
	string protocol = s1.substr(0, pl);
	cout << protocol << endl;
	// 2. 域名
	size_t p2 = s1.find("/", next += 3);
	string rn = s1.substr(next, p2 - next);
	next = p2;
	cout << rn << endl;

	// 3. 资源名
	string way = s1.substr(next);
	cout << way << endl;
	return 0;
}

12. getline

 Function: Used to read a row of data from the input stream and store it in a string object.

is: input stream object.

str: A string object used to store the read data.

delim: optional parameter, indicating the end of the line, the default is '\n' . (no space as terminator)

 For example, Niuke's question:

The length of the last word of a string

13. Convert strings to other data (C++11)

If you are interested, you can try one by one. Take a chestnut:

stoul : string to unsigned long integer 

Summary of the use of the string class

   1. Learn to check documents! ! ! ( important )

   2. You can ask chatgpt

   3. The more you practice, the better you will be

epilogue

This section is over here, thank you friends for browsing, if you have any suggestions, welcome to comment in the comment area; if you bring some gains to your friends, please leave your likes, your likes and concerns will become bloggers The driving force of the master's creation.

Guess you like

Origin blog.csdn.net/qq_72112924/article/details/131103529