STL beginner -- something about string class

Posted by isam4m on Thu, 30 Dec 2021 21:54:03 +0100

What is STL? STL refers to the Standard Template Library. It contains a large number of template classes and template functions. It is a collection of basic templates provided by C + +. It is used to complete functions such as input / output and mathematical calculation. Fundamentally, STL is a collection of containers, algorithms and other components How should we learn STL? Develop a good habit of reading relevant documents. Here are two good websites for friends (remember to be prepared to overcome your English reading difficulties!):

cplusplus is not an official website, but the layout is beautiful. It supports C++11
cppreference, the official website, can support the latest standards, but the layout is not very beautiful
The general structure and layout of the document are: interface function declaration - > description of functions, parameters and return values of interface functions - > use sample code

Let's officially enter the learning of string class!

First of all, the string class in STL has nine constructors. Here we will learn from the three most commonly used constructors

  1. Parameterless constructor of string class
  2. Parameterized constructor of string class
  3. Copy constructor of string class
string s1;             //Nonparametric structure 
string s2("come on");  //Structure with parameters
string s3(s2)          //copy construction 
string s4="come on";
s1 = "happy";   
//Equivalent to the construction with parameters, string supports the construction of char type strings

Operation results
Expand small knowledge:
1.wchar_t is a wide byte, and 2byte can better represent encoding such as unicode, which corresponds to wsting in STL, while the common is sting corresponding to char
2.ascll code is actually a code for getting up early in English. Later, in order to display the languages of various countries, unicode,utf-8,uft-16,uft-32 and other codes were introduced,
gbk is a special coding method for Chinese.

Next, let's further understand it through a simple exercise!

Original question link
Problem solving ideas:
Method 1, subscript + [] traversal and + count sorting idea

class Solution {
public:
    int firstUniqChar(string s) {
        int count[26]={0};
        for(size_t i=0;i<s.size();++i)
        {
            count[s[i]-'a']++;
        }
        for(size_t i=0;i<s.size();++i)
        {
            if(count[s[i]-'a']==1)
                return i; 
        }
        return -1;

    }
};

Method 2: iterator traversal + counting sorting idea

class Solution {
public:
    int firstUniqChar(string s) {
        int count[26]={0};
        string::iterator it=s.begin();
        while(it!=s.end())
        {
            count[*it-'a']++;
            ++it;
        }
        for(size_t i=0;i<s.size();++i)
        {
            if(count[s[i]-'a']==1)
            return i;
        }
        return -1;
    }
};

Method 3: range for traversal + counting sorting idea

class Solution {
public:
    int firstUniqChar(string s) {
        int count[26]={0};
        for(auto x: s)
        {
            count[x-'a']++;
        }
        for(size_t i=0;i<s.size();++i)
        {
            if(count[s[i]-'a']==1)
                return i;
        }
        return -1;
    }
};
To sum up, in fact, the only difference between the three methods is that the three traversal methods are different. The idea of counting and sorting is the idea of thinking.

Want to know more about the three traversal methods? Click here!

Finally, let's take a look at some other commonly used interfaces

string.erase() usage

  1. string.erase(pos,n) deletes n characters starting from pos (pos is an iterator of type string)
    Default pos=0,n=nps=-1
  2. string.erase(pos) deletes the character at pos
  3. string.erase(first,last) deletes the characters from first to last
string s1;
s1.erase(0,1);
s1.erase();    //Delete for all

Tail insertion

1.push_back('') inserts a single character
2.append("") inserts a string and can also be copied
3. Directly use + =, you can directly insert a single character or a string

string s1;
s1.push_back('i');
s1.append("world");
string s2;
s2.append(s1);
s1+="rh";           //In fact, the first two interfaces are called at the bottom
s1+='l';

Head or middle insertion

string.insert()
Note that inserting a single character is not supported, but a one character string can be inserted

string s1;
s1.insert(0,"x");     //Head insert
s1,insert(3,"yyyy");  //Intermediate insertion

Although header insertion and intermediate insertion can be performed, insert should be used as little as possible, because the underlying implementation is array, and header insertion or intermediate insertion needs to move data.

Topics: C++ Back-end