[C++ basic knowledge] string class

string class

Constructor:

string(); //default constructor
string (const string& str); //copy constructor
string (const string& str, size_t pos, size_t len ​​= npos);//substring constructor
string (const char* s) ; //Use C language style string to construct
string (const char* s, size_t n); //Copy the first n characters from the character array pointed to by s.
string (size_t n, char c);//Fill the string with n consecutive copies of character c.

template <class InputIterator>
string (InputIterator first, InputIterator last);//Use an iterator to construct a string

Example:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;
void test()
{
    
    
	string str;
	string str1("abcdefgh");
	string str2(str1);
	string str3(str1,0,5);
	string str4("zxcvbnm",5);
	string str5(5,'x');
}

int main()
{
    
    
	test();
	return 0;
}

result:
Insert picture description here

Capacity operation interface for string objects

Function name Features
size Returns the effective character length of the string
length Returns the effective character length of the string
capacity Return the size of the capacity
empty Check whether the string is empty, return true if yes, false if no
clear Clear valid characters
reserve Adjust the size of the string capacity
resize Set the number of valid characters to n, and fill the extra space with character c

note:

  1. The underlying implementation principle of size() and length() is exactly the same. The reason for introducing size() is to be consistent with the interfaces of other containers. In general, size() is basically used.
  2. clear() only clears the valid characters in the string without changing the size of the underlying space.
  3. Both resize(size_t n) and resize(size_t n, char c) change the number of valid characters in a string to n, the difference is that when the number of characters increases: resize(n) is filled with 0 Element space, resize(size_t n, char c) uses character c to fill the extra element space.
    Note: When resize changes the number of elements, if the number of elements is increased, the size of the underlying capacity may be changed. If the number of elements is reduced, the total size of the underlying space remains unchanged.
  4. reserve(size_t res_arg=0): Reserve space for string without changing the number of effective elements. When the parameter of reserve is less than
    the total size of the underlying space of string, the server will not change the capacity.

string::operator+=

+= is equivalent to the operation of tail insertion
string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
    
    
	string str1;
	string str2 = "123";
	str1 += str2;//123
	str1 += "abc";//123abc
	str1 += 'x';//123abcx

	return 0;
}

Insert picture description here

string::append

Expand the string by appending other characters to the end of the current value of the string

string& append (const string& str);
string& append (const string& str, size_t subpos, size_t sublen);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);

template <class InputIterator>

string& append (InputIterator first, InputIterator last);

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;

int main()
{
    
    
	string str;
	string str2 = "123";
	
	//string& append(const string& str);
	str.append(str2);//123
	str.append(str2);//123123
	cout << str << endl;

	//string& append(const string& str, size_t subpos, size_t sublen);
	str.append(str2,0,2);//12312312
	cout << str << endl;


	//string& append(const char* s);
	str.append("abc");//12312312abc
	cout << str << endl;


	//string& append(const char* s, size_t n);
	str.append("zxcvbnm",4);//12312312abczxcv
	cout << str << endl;

    //string& append(size_t n, char c);
	str.append(3,'x');//12312312abczxcvxxx
	cout << str << endl;

	/*template \<class InputIterator>
		string& append (InputIterator first, InputIterator last);*/
	str.append(str2.begin(),str2.end());//12312312abczxcvxxx123
	cout << str << endl;

	char s[] = "369";
	str.append(s, s + sizeof(s) / sizeof(s[0]));//12312312abczxcvxxx123369
	cout << str << endl;

	return 0;
}

Output result:
Insert picture description here

string::insert

string& insert (size_t pos, const string& str);
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
string& insert (size_t pos, const char* s);
string& insert (size_t pos, const char* s, size_t n);
string& insert (size_t pos, size_t n, char c);
void insert (iterator p, size_t n, char c);
iterator insert (iterator p, char c);
template <class InputIterator>

void insert (iterator p, InputIterator first, InputIterator last);

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
using namespace std;


int main()
{
    
    
	string str = "mm";
	string str2 = "123";
	//string& insert(size_t pos, const string& str);
	str.insert(1,str2);//m123m
	cout << str << endl;

	//string& insert(size_t pos, const string& str, size_t subpos, size_t sublen);
	str.insert(1,str2,1,2);//m23123m
	cout << str << endl;

	//string& insert(size_t pos, const char* s);
	str.insert(0,"abc");//abcm23123m
	cout << str << endl;

	//string& insert(size_t pos, const char* s, size_t n);
	str.insert(0,"000000",5);//00000zbcm23123m
	cout << str << endl;

	/*string& insert(size_t pos, size_t n, char c);
	void insert(iterator p, size_t n, char c);*/
	str.insert(str.begin(),3,'x');//xxx00000zbcm23123m
	cout << str << endl;

	//iterator insert(iterator p, char c);
	
	/*template \<class InputIterator>
		>> void insert(iterator p, InputIterator first, InputIterator last);*/
	str.insert(str.end(),str2.begin(),str2.end());//xxx00000zbcm23123m123

	cout << str << endl;


}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/115005615