STL----string class

@TOC

     

Table of contents

1. Constructor

2. Destructor

3.Iterator

   4. Memory function

 5.Element access function

    (1) operator[] function

   (2) at function

  (3) back function and front function

     6. Modify content function

        (1) Additional functions

(2) insert function

(3)erase function

 (4)replace function

(5)pop_back function

7. String operation functions

(1) c_str function

(2) find function


  STL: 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.

 Today, we are talking about strings in STL containers.


1. Constructor

           

       The use of these seven constructors is as follows:

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

int main()
{
	//string (); 
	//构造一个空字符串,长度为0个字符
	string s1;
	//string (const char* s);
	//复制s指向的以空结束的字符序列
	string s2("hello world");
	//string (const string& str);  
	//构造一个str的复制
	string s3(s2);
	//string (const string& str, size_t pos, size_t len = npos);  
	//复制str中从字符位置pos开始并跨越len个字符的部分。(如果str太短或len为string::npos,则复制到str的末尾)
	string s4(s2, 2, 2);
	//string (const char* s, size_t n);
	//复制s指向的字符串的前n个字符
	string s5("hello world", 5);
	//string (size_t n,char c);
	//用n个字符c来填充string
	string s6(7, 's');
	//string (InputIterator first, InputIterator last);
	//以相同的顺序复制范围[first, last)中的字符序列
	string::iterator begin = s5.begin();
	string::iterator end = s5.end();
	string s7(begin, end);
	cout << s1 << endl;
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;
	cout << s5 << endl;
	cout << s6 << endl;
	cout << s7 << endl;
	return 0;
}

2. Destructor

      

3.Iterator

        There are 8 types of iterators:

                                  1. The iterator of the starting position of the general label,

                                  2. The iterator at the end of the end of the String,

                                  3. Mark the reverse iterator of the back position of String,

                                  4. A reverse iterator that marks the reverse end position of string,

                                  5. Mark the Const iterator at the starting position of String,

                                  6. Mark the Const iterator at the end position of String.

                                  7. Mark the reverse CONST iterator of the back position of String,

                                  8. Mark the reverse CONST iterator at the back -ending position of String.

int main()
{
	string s("hello world");
	for (string::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it;
	}
	cout << endl;

	for (string::reverse_iterator it = s.rbegin(); it != s.rend(); it++)
	{
		cout << *it;
	}
	cout << endl;
    

	return 0;
}

   4. Memory function

          

       Among these functions related to memory, size() and length() belong to the same functional function. We often use size() when using it, because in other containers, size() is used instead of length().

       In addition, max_size() represents the maximum length of a string. This is a function that returns a fixed value and is not very useful.

        The remaining functions are very useful, such as capacity()

       You will find that the capacity of s is 15, 31, 47, 70... Of course, this only changes in the Visual Studio compiler. In different systems, the changes are different.

       The resize() and reverse() functions are two expansion functions. If the data you input is smaller than the existing string capacity, it will only reduce the size but not the capacity. If the input data is larger than the existing string capacity, it will only reduce the size. When there is a string capacity, it will be expanded until the capacity is greater than the input desired capacity.

        The empty() function is used to determine whether the string content is empty, and the clear() function is used to clear the string content.

 5.Element access function

       

    (1) operator[] function

char& operator[] (size_t pos);
//或者
const char& operator[] (size_t pos) const;

Note: If pos is equal to the length of the string, and the string is const qualified, the function returns a reference to the null character ('\0').

The operator[] function is an operator overloaded function and is used as follows:

int main()
{
	string s("hello world");
	for (size_t i = 0; i < s.size(); i++)
	{
		cout << s[i] << " ";
	}
	cout << endl;
	return 0;
}

   (2) at function

char& at (size_t pos);
//或者
const char& at (size_t pos) const;

       This function can return a reference to the character at pos position in the string.

       At the same time, it automatically checks whether pos is a valid position of a character in the string (that is, whether pos is less than the length of the string), and if not, an out_of_range exception is thrown.

   Use as follows:

int main()
{
	string s("hello world");
	for (size_t i = 0; i < s.size(); i++)
	{
		cout << s.at(i) << " ";
	}
	cout << endl;
	return 0;
}

  (3) back function and front function

char& back();
//或者
const char& back() const;



char& front();
//或者
const char& front() const;

       The back function returns a reference to the last character of a string·

       The front function can return a reference to the first character of a string

       The front function returns a direct reference, unlike the iterator whose string::begin member returns the same character.​ 

       Note: The back and front functions cannot be called by empty strings

     6. Modify content function

        (1) Additional functions

                 operator+= function

int main()
{
	string s("hello world");
	string str = "@@@";
	//string& operator+=(const char* str);
	s += "!!!";
	//string& operator+=(const string& str);
	s += str;
	//string& operator+=(char c);
	s += '@';
	cout << s << endl;
	return 0;
}

               append function

int main()
{
	char* arr = "hello world ";
	//string & append(const char* s);
	//附加由s指向的以空结束的字符序列组成的字符串的拷贝。
	string s1;
	s1.append(arr);
	//string& append(const string & str);
	//追加str的拷贝
	string s2;
	s2.append(s1);
	//string& append(const string & str, size_t subpos, size_t sublen);
	//附加str的子字符串的拷贝。
	//子字符串是str的一部分,从字符位置subpos开始并跨越子字符(或者直到str的末尾,如果str太短或如果sublen是string::npos)
	string s3;
	s3.append(s1, 1, 3);
	//string& append(const char* s, size_t n);
	//附加s指向的字符数组中前n个字符的拷贝
	string s4;
	s4.append(arr, 5);
	//string& append(size_t n, char c);
	//追加字符c的n个连续拷贝
	string s5;
	s5.append(5, 'c');
	return 0;
}

           push_back function

void push_back(char c);

Function: Append character c to the end of the string, increasing its length by 1

int main()
{
	string s;
	s.push_back('h');
	s.push_back('e');
	s.push_back('l');
	s.push_back('l');
	s.push_back('o');
	cout << s << endl;
	return 0;
}

(2) insert function

            ​ ​ ​ ​ Unlike the additional function, the function of insert is more powerful.

//插入str的拷贝
string& insert (size_t pos, const string& str);

//插入str的子字符串的拷贝。子字符串是str的一部分,从字符位置subpos开始并跨越子字符(或者直到str的末尾,如果str太短或如果sublen为npos)。
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);

//插入由s指向的以空结束的字符序列组成的字符串的拷贝。
string& insert (size_t pos, const char* s);

//在s指向的字符数组中插入前n个字符的拷贝。
string& insert (size_t pos, const char* s, size_t n);

//插入字符c的n个字符拷贝
string& insert (size_t pos,   size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);

//插入字符c
iterator insert (const_iterator p, char c);

//以相同的顺序插入范围[first, last)中的字符序列的副本。
template <class InputIterator>iterator insert (iterator p, InputIterator first, InputIterator last);

(3)erase function

int main()
{
	string s = "hello world";
	s.erase(6, 5);
	cout << s << " " << s.size() << endl;
	string::iterator it1 = s.begin();
	s.erase(it1);
	cout << s << " " << s.size() << endl;
	string::iterator it2 = s.begin();
	string::iterator it3 = s.end();
	s.erase(it2, it3);
	cout << s << " " << s.size() << endl;

	return 0;
}

 (4)replace function

//拷贝str
string& replace (size_t pos, size_t len, const string& str);
string& replace (const_iterator i1, const_iterator i2, const string& str);

//复制str中从字符位置subpos开始并跨越子字符的部分(如果str太短或subblen为string::npos,则复制到str的末尾)。
string& replace (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen);

//复制s指向的以空结束的字符序列
string& replace (size_t pos, size_t len, const char* s);
string& replace (const_iterator i1, const_iterator i2, const char* s);

//从s指向的字符数组中复制前n个字符
string& replace (size_t pos, size_t len, const char* s, size_t n);
string& replace (const_iterator i1, const_iterator i2, const char* s, size_t n);
	
//用字符c的n个连续拷贝替换字符串的一部分
string& replace (size_t pos, size_t len, size_t n, char c);
string& replace (const_iterator i1, const_iterator i2, size_t n, char c);

//以相同的顺序复制范围[first, last)中的字符序列
template <class InputIterator>  string& replace (const_iterator i1, const_iterator i2,                   InputIterator first, InputIterator last);

(5)pop_back function

void pop_back();

Function: Erases the last character of a string, effectively reducing its length by 1.

The operation is similar to push_back

7. String operation functions

(1) c_str function

        Returns a pointer to an array containing a null-terminated sequence of characters (i.e. a C-string) representing the current value of the string object.

        This array contains the same sequence of characters that make up the value of the string object, plus an additional terminating null character ('\0') at the end.

(2) find function

Searches a string for the first occurrence of the sequence specified by its argument.

When pos is specified, the search only includes characters at or after pos and ignores any characters that may appear before pos.

Note that unlike the member find_first_of, whenever multiple characters are searched, it is not enough to match just one of them, the entire sequence must match.


    The above STL function is a common function of string. If you want to know other functions, you can learn it on the cplusplus website. The next blog will talk about the simple implementation of string.

Guess you like

Origin blog.csdn.net/m0_62812354/article/details/128500327