STL Unit 1 String container

Keywords: C++ STL

1. string container

1.1 string concept

Essence:

String is a C + + style string, and string is essentially a class.
The difference between string and char *:

Char * is a pointer, string is a class, and char * is encapsulated inside the class. Managing this string is a char * type container.

string manages the memory allocated by char *. There is no need to worry about copy out of bounds and value out of bounds. It is the responsibility of the class.

 

one point two   Constructor

        Function prototype:

Function prototypefunction
  string();Create an empty string.
string(const char* s);Initialize with string s.
string(const string& str);Initializes another string object with one string object.
string(int n, char c);Initialize with n characters c.

      Code example
    

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

int main()
{
	string s1;	                          //Create an empty string.
	const char* str = "Hello C++";      //  string(const char* s);   Initialize with string s.
	string s2(str);                       
     cout << s2 << endl;

	string s3(s2);                          // Initializes another string object with one string object.
	cout << s3 << endl;                  

	string s4(10,'a');                    //Initialize with 10 characters' a '
	cout << s4 << endl;
}

1.3 string assignment

  Function prototype:

Function prototypefunction
string& operator=(const char* s);char * type string is assigned to the current string.
string& operator=(const string &s);Assign the string s to the current string.
string& operator=(char c);The character is assigned to the current string.
string& assign(const char *s);Assign the string s to the current string.
string& assign(const char *s, int n);Assign the first n characters of string s to the current string
string& assign(const string &s);Assigns the string s to the current string.
string& assign(int n, char c);Assign n characters c to the current string.

Code example

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


int main()
{
	string str1;
	str1 = "Hello C++";
   cout << str1 << endl;

	string str2;
	str2 = str1; 
    cout << str2 << endl;                      // Assign the string s to the current string

	string str3;
	str3 = 'a';    
	cout << str3 << endl;                     //The character is assigned to the current string

 /*	string str4;
	str4.assign("Hello C++");  
    cout << str4 << endl;                        // Assign the string s to the current string  
 */                                         //   Personally, I think it's better to directly str4 = "Hello C + +";

	string str5;
	str5.assign("Hello C++", 4); 
     cout << str5 << endl;          //Assign the first n characters of string s to the current string. Note that it is assigned to
                                    // Cannot be written as str5.assign(str1, 4); See function prototype for the reason 
                                    //Function prototype string & assign (const char * s, int n);
                                    //  Distinguish it from the attachment splicing below

  


/* string str6;
	 str6.assign(str5);                               //Assign the string s to the current string     
     cout << str6 << endl;                    //It is suggested here that str6 = str5; Relatively simple
 */
	string str7;
	str7.assign(10,'w');    
	cout << str7 << endl;                      //Assign n characters c to the current string


}

1.4 string splicing

Function prototype for splicing strings at the end of strings:

Function prototype
string& operator+=(const char* str);Overload the + = operator.
string& operator+=(const char c);Overload the + = operator.
tring& operator+=(const string& str);Overload the + = operator.
string& append(const char *s);Concatenates the string s to the end of the current string.
string& append(const char *s, int n);Connect the first n characters of string s to the end of the current string.
string& append(const string &s);Concatenates the string s to the end of the current string.
string& append(const string &s, int pos, int n);n characters from pos in string s are connected to the end of the string.

Example:

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

int main()
{
     
    string s1="welcome";
    s1+="study";      //Overload + = operator
    cout<<s1<<endl;
   
    s1+=':';            //Overload + = operator
    cout<<s1<<endl;

    string s2="STL";
    s1+=s2;           // Overload + = operator s1 = "welcome to learn: STL";
    cout<<s1<<endl;


    string s3="I ";
    s3.append("want to");  // Connects the string s to the end of the current string
    cout<<s3<<endl;

    s3.append(" be an acmer abcd",12);   // Connect the first n characters of string s to the space at the end of the current string
    cout<<s3<<endl;

    s3.append(s1);                //Connects the string s to the end of the current string
    cout<<s3<<endl;


    s3.append(s2,0,3);    // The n characters from pos in string s are connected to the end of the string, starting from bit 0
     cout<<s3<<endl;
 
 

}
		
	

1.5 string find and replace

Function prototype:

Function modelfunction
int find(const string& str, int pos = 0) const;Find the location where str first appears, starting from pos.
int find(const char* s, int pos = 0) const;Find the location where s first appears, starting from pos.
int find(const char* s, int pos, int n) const;Find the first position of the first n characters of s from the pos position.
int find(const char c, int pos = 0) const;Find where the character c first appears.
int rfind(const string& str, int pos = npos) const;Find the last position of str, starting from pos.
int rfind(const char* s, int pos = npos) const;Find the location where s last appeared, starting from pos.
int rfind(const char* s, int pos, int n) const;Find the last position of the first n characters of s from pos.
int rfind(const char c, int pos = 0) const;Finds the last occurrence of the character c.

Example:

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

int main()
{
     string s1="hello acm acm";
     
     int pos1 =s1.find("cm");   //Return - 1 if not found 
     cout<<pos1<<endl;
     
     int pos2=s1.find("cm",7,2);  //Find the first position of the first n (1) characters of s (cm) from the pos (1) position.
	 cout<<pos2<<endl;
	 
	 int pos3=s1.rfind("acm");    //Find the location where s last appeared, starting from pos.
	 cout<<pos3<<endl;
	 string s2="acm";
	 int pos4=s1.rfind(s2);            //Find the last position of str, starting from pos.
	 cout<<pos4<<endl;    


}

Function model to replace the string at the specified position:

Function prototypefunction
string& replace(int pos, int n, const string& str);Replace n characters starting from pos with the string str.
string& replace(int pos, int n,const char* s);Replace the n characters starting from pos with the string s.

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

int main()
{
   		string s1="abcdef";
        string s2;
		s2=s1.replace(0,3,"1111");//Three characters starting from 0 replace bit 0, which is also a number of 1 1111def 
		cout<<s2<<endl;
    
]

1.6 string comparison

Function model for comparing string sizes:

Function modelfunction
int compare(const string &s) const;Compare with string s.
int compare(const char *s) const;Compare with string s.

Example:

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

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

	//Compare with string s 
	if (s1.compare(s2) == 0)	
       cout << "=" << endl;
	else if (s1.compare(s2) > 0)	
      cout << ">" << endl;
	else cout << "<" << endl;

}

1.7 string character access

Function model of single character access in string:

The subscript operator [] does not check the validity of the index when used. If the subscript exceeds the length range of characters, it will lead to undefined behavior.

When the function at() is used, it will check whether the subscript is valid and give a prompt     out_of_range exception

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

int main()
{
   string s1="succes need patient";
   for(i=0;i<s1.size();i++)
  {
    cout <<s1[i]<<endl;            //  cout<<s1.at[i]<<endl;

  }
   cout<<endl;

    

   s1[0]='S';              //s1.at[0]='S';
   cout<<s1<<endl;

   char c=s1[0];           //char c=s.at[0]
   cout<<c<<endl;

}

1.8 string insertion and deletion

Function prototype for inserting and deleting characters from a string:

                     The starting subscript of insertion and deletion starts from 0.

Function modelfunction
string& insert(int pos, const char* s);Insert string.
string& insert(int pos, const string& str);Insert string.
string& insert(int pos, int n, char c);Inserts n characters c at the specified position.
string& erase(int pos, int n = npos);Delete n characters starting from Pos.

Example:

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

int main()
{
   string s1="icpc acm";
   string s2=s1.substr(4,8);
   cout<<s2<<endl;

  //Cooperate with find to obtain a certain format
  string s3="acmicpc@163.com";
   int pos=s3.find('@');
  string s4=s3.substr(0,pos);
   cout<<s4<<endl;


}

Posted by redbrad0 on Fri, 17 Sep 2021 22:49:56 -0700