3. C++ string container - learning notes

3. string container

3.1 basic concept of string

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. Char * is enclosed inside the class. It is a container of char * type to manage this string

3.2. string constructor

#include <iostream>
using namespace std;

//string constructor

/*
 * string() ;           //Create an empty string
 * string(const char* s); //Initialize with string s
 * string(const string& str) ; //Initializes a string object with a string object
 * string(int n, char c); //Initialize with n characters c
 */

int main()
{
    string s1; //Default construction

    const char* str = "Hello !";
    string s2(str);
    cout << "s2 = " << s2 << endl;

    string s3(s2);
    cout << "s3 = " << s3 << endl;

    string s4(10,'c');
    cout << "s4 = " << s4 << endl;
    return 0;
}

3.3. string assignment

Function prototype assigned:

  • 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); // Assign the string s to the current string
  • string& assign(int n, char c); // Assign n characters C to the current string
#include <iostream>
using namespace std;

//string assignment operation
/*
 * Function prototype assigned:

string& operator=(const char* s); //char*The 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); //Assign the string s to the current string
string& assign(int n, char c); //Assign n characters c to the current string
 */
int main()
{
    string str1;
    str1 = "hello HH";
    cout << "str1 = " << str1 << endl;

    string str2;
    str2 = str1;
    cout << "str2 = " << str2 << endl;

    string str3;
    str3 = 'a';
    cout << "str3 = " << str3 << endl;


    string str4;
    str4.assign("Hello af");
    cout << "str4 = " << str4 << endl;

    string str5;
    str5.assign("heelj",2);
    cout << "str5 = " << str5 << endl;

    string str6;
    str6.assign(str5);
    cout << "str6 = " << str6 << endl;

    string str7;
    str7.assign(10,'c');
    cout << "str7 = " << str7 << endl;


    return 0;
}

3.4 string splicing

Function prototype:

  • string& operator+=(const char* str); // Overload + = operator
  • string& operator+=(const char c); // Overload + = operator
  • string& operator+=(const string& str); // Overload + = operator
  • string& append(const char *s); // Connects 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); // Same as operator + = (const string & STR)
  • string& append(const string &s, int pos, int n);// N characters from POS in string s are connected to the end of the string
#include <iostream>
using namespace std;

/*
 * **Function prototype:**

- `string& operator+=(const char* str);` //Overload + = operator
- `string& operator+=(const char c);` //Overload + = operator
- `string& operator+=(const string& str);` //Overload + = operator
- `string& append(const char *s);` //Connects 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);` //Same as operator + = (const string & STR)
- `string& append(const string &s, int pos, int n);`//n characters from pos in string s are connected to the end of the string
 */
int main()
{
    //String splicing
    string str1 = "I";
    str1 += "Love reading";
    cout << "str1 = " << str1 << endl;

    str1 += ':';
    cout << "str1 = " << str1 << endl;

    string str2 = "BOOK";
    str1 += str2;
    cout << "str1 = " << str1 << endl;


    string str3 = "I";
    str3.append(" Love");
    cout << "str3 = " << str3 << endl;

    str3.append(" and Game",4);
    cout << "str3 = " << str3 << endl;

    //Intercept from subscript 5 to subscript 9
    str3.append(" and Game" , 5,9);
    cout << "str3 = " << str3 << endl;
    return 0;
}

3.5. string search and replacement

Function Description:

  • Find: finds whether the specified string exists
  • Replace: replaces the string at the specified location

Function prototype:

  • 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 of the last occurrence of S, 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; // Find where the character c last appeared
  • string& replace(int pos, int n, const string& str); // Replace n characters starting from POS with string str
  • string& replace(int pos, int n,const char* s); // Replace the n characters starting from POS with the string s

be careful:

  • find is from left to back, and rfind is from right to left
  • find returns the first character position after finding the string. If it is not found, it returns - 1
  • When replacing, you need to specify where to start, how many characters to replace and what kind of string to replace
#include <iostream>
using namespace std;

int main()
{
    //Find and replace
    string str1 = "abcdefgde";
    int pos = str1.find("dec");

    //Search from left to right. The first index to be found. If it cannot be found, it returns - 1
    cout << "pos = " << pos << endl;
    if (pos == -1)
    {
        cout << "not found" << endl;
    }
    else
    {
        cout << "pos = " << pos << endl;
    }

    pos = str1.rfind("de");
    //rfing is looking from right to left
    cout << "pos = " << pos << endl;


    //replace
    string str2 = "abcdefgde";
    //Replace the position of subscript from 0 to 3 with HHH
    str2.replace(0,3 , "HHHHH");
    cout << "str2 = " << str2 << endl;
    return 0;
}

3.6. string comparison

Function Description:

  • Comparison between strings

Comparison method:

  • String comparison is based on the ASCII code of characters

=Return 0

>Return 1

< return - 1

Function prototype:

  • int compare(const string &s) const; // Compare with string s

  • int compare(const char *s) const; // Compare with string s

    #include <iostream>
    using namespace std;
    
    int main()
    {
        //string comparison
    
        string s1 = "hello";
        string s2 = "zello";
    
        int ret = s1.compare(s2);
        if(ret == 0)
        {
            cout << "s1 == s2" << endl;
        }
        else if(ret > 0)
        {
            cout << "s1 > s2" << endl;
        }else
        {
            cout << "s1 < s2 " << endl;
        }
    
    
        return 0;
    }
    

3.7 string character access

There are two ways to access a single character in a string

  • char& operator[](int n); // Take characters by []
  • char& at(int n); // Get characters through the at method
#include <iostream>
using namespace std;

int main()
{
    //Character access
    string str = "hello world";

    //Accessed via []
    for(int i = 0 ; i < str.size() ; i++)
    {
        cout << str[i] << " " ;
    }
    cout << endl;

    //Access via at()
    for(int i = 0 ; i < str.size() ; i++)
    {
        cout << str.at(i) << " ";
    }
    cout << endl;

    //Character modification
    str[0] = 'Z';
    str.at(1) = 'Z';
    cout << "After character modification str = " << str << endl;
    return 0;
}

3.1.8 string insertion and deletion

Function Description:

  • Insert and delete characters from a string

Function prototype:

  • 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
#include <iostream>
using namespace std;

int main()
{

    //String insertion and deletion
    string str = "hello";
    str.insert(1, "222");
    cout << str << endl;
    
    //[0,1)
    str.insert(0,1,'C');
    cout << str << endl;

    //Delete [0,4)
    str.erase(0,4);
    cout << "Deleted str = " << str << endl;
    return 0;
}

3.9 string substring

Function Description:

  • Get the desired substring from the string

Function prototype:

  • string substr(int pos = 0, int n = npos) const; // Returns a string of n characters starting with POS
#include <iostream>
using namespace std;

int main()
{
    //Substring interception
    string str = "abcdefg";
    string subStr = str.substr(0,3);
    cout << "intercept[0,4) = " << subStr << endl;


    string email = "hello@sina.com";
    //Found subscript for @
    int pos = email.find("@"); 
    //String up to @
    string username = email.substr(0 , pos);
    cout << "username = " << username << endl;
    return 0;
}

Added by marsupillami on Mon, 03 Jan 2022 19:22:39 +0200