C++ STL summary string

1.string memory structure

String is not a basic container. It is classified as a string Library in CppReference. In fact, it is std::basic_string. The bottom layer adopts the storage structure of vector, so the memory structure is the same as vector.

2.string capacity operation

functionfunction
bool empty()Check whether the container is empty
size_t size()Returns the number of elements in the container
size_t length()Returns the number of characters, equivalent to size()
size_t max_size()Returns the maximum number of elements that the container can hold due to the implementation of the system or library
size_t capacity()Returns the number of elements that can be accommodated in the container's allocated space
void reserve(size_t new_cap)Increase the capacity of the container, if new_ If the capacity is greater than capacity(), new storage will be allocated; otherwise, no processing will be performed
void resize(size_t new_size, const T& x)Reset the size of the container. If the size becomes larger, fill the longer position with the default value. If the size becomes smaller, delete the tail element of the shorter part
void shrink_to_fit()Request to remove unused capacity from container
void test_capacity(){
        string test;
        cout << "*************************after init****************************\n";
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after push_back****************************\n";
        test.push_back('a');
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after reserve****************************\n";
        test.reserve(20);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after resize****************************\n";
        test.resize(5);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

        cout << "*************************after shrink_to_fit****************************\n";
        test.shrink_to_fit();
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  length(): "<< test.length() << "  max_size(): " << test.max_size() << "  capacity(): " << test.capacity() << endl;

}

result:

*************************after init****************************
empty(): 1  size(): 0  length(): 0  max_size(): 4611686018427387903  capacity(): 15
*************************after push_back****************************
empty(): 0  size(): 1  length(): 1  max_size(): 4611686018427387903  capacity(): 15
*************************after reserve****************************
empty(): 0  size(): 1  length(): 1  max_size(): 4611686018427387903  capacity(): 30
*************************after resize****************************
empty(): 0  size(): 5  length(): 5  max_size(): 4611686018427387903  capacity(): 30
*************************after shrink_to_fit****************************
empty(): 0  size(): 5  length(): 5  max_size(): 4611686018427387903  capacity(): 15

3.string structure

functionfunction
string()Default constructor, construct an empty string
string(size_t n, char ch)Construct a string with n values of ch
string(const string &other, size_t pos, size_t count==npos)Construct a string with the substring [POS, POS + count] of other
string(const char* s , size_t count)Construct a string with the first count characters of s
string(const char* s)Construct a string with the characters in s
string( interator first, interator last )Construct a string with the contents of the range [first, last]
string( const string& other )Copy constructor, which constructs a container with other content with copy semantics
string( string&& other )The mobile constructor constructs a container with other content with mobile semantics. After moving, other is empty()
string( std::initializer_list init)Construct a string with initializer_list init content
void test_construct(){
        cout << "*************************string()****************************\n";
        string  test;
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(10,'a')****************************\n";
        string test1(10,'a');
        for(int i=0; i<test1.size(); ++i){
                cout << test1[i] << "  ";
        }
        cout << endl;

        string test2("abcdefghijkl");
        cout << "*************************string(test2, 1,5)****************************\n";
        string test3(test2, 1 ,5);
        for(int i=0; i<test3.size(); ++i){
                cout << test3[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(s, 5)****************************\n";
        const char* s = "hello world";
        string test4(s, 5);
        for(int i=0; i<test4.size(); ++i){
                cout << test4[i] << "  ";
        }
        cout << endl;
        
        cout << "*************************string(s)****************************\n";
        string test5(s);
        for(int i=0; i<test5.size(); ++i){
                cout << test5[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(test2.begin(), test2.begin()+3)****************************\n";
        string test6(test2.begin(), test2.begin()+3);
        for(int i=0; i<test6.size(); ++i){
                cout << test6[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(test2)****************************\n";
        string test7(test2);
        for(int i=0; i<test7.size(); ++i){
                cout << test7[i] << "  ";
        }
        cout << endl;

        cout << "*************************string(std::move(test2))****************************\n";
        string test8(std::move(test2));
        for(int i=0; i<test8.size(); ++i){
                cout << test8[i] << "  ";
        }
        cout << endl;

        cout << "*************************string{'1','2','3','4','5'}****************************\n";
        string test9{'1','2','3','4','5'};
        for(int i=0; i<test9.size(); ++i){
                cout << test9[i] << "  ";
        }
        cout << endl;
}
*************************string()****************************

*************************string(10,'a')****************************
a  a  a  a  a  a  a  a  a  a  
*************************string(test2, 1,5)****************************
b  c  d  e  f  
*************************string(s, 5)****************************
h  e  l  l  o  
*************************string(s)****************************
h  e  l  l  o     w  o  r  l  d  
*************************string(test2.begin(), test2.begin()+3)****************************
a  b  c  
*************************string(test2)****************************
a  b  c  d  e  f  g  h  i  j  k  l  
*************************string(std::move(test2))****************************
a  b  c  d  e  f  g  h  i  j  k  l  
*************************string{'1','2','3','4','5'}****************************
1  2  3  4  5  

4.string element access

functionfunction
at()Access the specified character and perform an out of bounds check
operator[]Access specified characters
front()Access first character
back()Access last character
data()Returns a pointer to the first character in memory
c_str()Returns a non modifiable C-style character array
void test_access(){
        string test("abcdefghijkl");
        cout<< "at(2):" << test.at(2) << "  [3]:"<< test[3] << "  front:" << test.front() << "  back:" << test.back() << "  *data:" << *test.data()
                << "  c_str():" << test.c_str() << endl;
}
at(2):c  [3]:d  front:a  back:l  *data:a  c_str()abcdefghijkl

5.string element modification

functionfunction
push_back(char ch)Insert new character ch at the end
insert(iterator pos, char ch)Insert new character ch before pos
insert(iterator pos, size_t n, char ch)Insert n new characters ch before pos
insert(iterator pos, iterator first, iterator last)Insert characters from the range [first, last] before pos
erase(size_t index0, size_t countnpos)Remove count characters from index
erase(iterator pos)Remove character at pos
erase(iterator first, iterator last)Remove characters from the range [first, last]
append(size_t n, char ch)n ch attached
append(const string &str)string str attached
append(const string &str, size_t pos, size_t count==npos)Substring of str attached [pos, pos+count)
append(const char* s , size_t count)The first count characters of s are attached
append(const char* s)Attached string s
append( interator first, interator last )The contents of the scope [first, last) are attached
append( std::initializer_list init)The contents of initializer_list init are attached
operator+=( char ch)Attached ch
operator+=(const string &str)string str attached
operator+=(const char* s)Attached string s
operator+=( std::initializer_list init)The contents of initializer_list init are attached
compare(const string &str)Compare with str
pop_back()Delete trailing characters
swap(vector& other)Swap content with other. No move, copy, or swap operations are called on a single element
clear()Clear all elements
void test_modifier(){
        string test("12345");
        cout << "*************************push_back('a')****************************\n";
        test.push_back('a');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),'b')****************************\n";
        test.insert(test.begin(), 'b');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),3,'c')****************************\n";
        test.insert(test.begin(), 3, 'c');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),test2.begin(),test2.end())****************************\n";
        string test2("abcde");
        test.insert(test.begin(), test2.begin(), test2.end());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(1,3)****************************\n";
        test.erase(1,3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin())****************************\n";
        test.erase(test.begin());
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin(),test.begin()+2)****************************\n";
        test.erase(test.begin(), test.begin()+2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(3,'a')****************************\n";
        test.append(3, 'a');
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2)****************************\n";
        test.append(test2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2, 1,3)****************************\n";
        test.append(test2, 1, 3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(s, 3)****************************\n";
        const char* s = "lmnopq";
        test.append(s,3);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(s)****************************\n";
        test.append(s);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append(test2.begin(),test2.begin()+2)****************************\n";
        test.append(test2.begin(), test2.begin()+2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************append({'3','2','1'})****************************\n";
        test.append({'3','2','1'});
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=('a')****************************\n";
        test += 'a';
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=(test2)****************************\n";
        test += test2;
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+=(\"aaaaa\")****************************\n";
        test += "aaaaa";
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************+={'3','2','1'}****************************\n";
        test +={'3','2','1'};
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************compare(test2)****************************\n";
        cout << test.compare(test2) << std::endl;

        cout << "*************************pop_back()****************************\n";
        test.pop_back();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************test.swap(test2)****************************\n";
        test.swap(test2);
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;

        cout << "*************************clear()****************************\n";
        test.clear();
        for(int i=0; i<test.size(); ++i){
                cout << test[i] << "  ";
        }
        cout << endl;
}

*************************push_back('a')****************************
1  2  3  4  5  a  
*************************insert(test.begin(),'b')****************************
b  1  2  3  4  5  a  
*************************insert(test.begin(),3,'c')****************************
c  c  c  b  1  2  3  4  5  a  
*************************insert(test.begin(),test2.begin(),test2.end())****************************
a  b  c  d  e  c  c  c  b  1  2  3  4  5  a  
*************************erase(1,3)****************************
a  e  c  c  c  b  1  2  3  4  5  a  
*************************erase(test.begin())****************************
e  c  c  c  b  1  2  3  4  5  a  
*************************erase(test.begin(),test.begin()+2)****************************
c  c  b  1  2  3  4  5  a  
*************************append(3,'a')****************************
c  c  b  1  2  3  4  5  a  a  a  a  
*************************append(test2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  
*************************append(test2, 1,3)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  
*************************append(s, 3)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  
*************************append(s)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  
*************************append(test2.begin(),test2.begin()+2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  
*************************append({'3','2','1'})****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  
*************************+=('a')****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  
*************************+=(test2)****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  
*************************+=("aaaaa")****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  
*************************+={'3','2','1'}****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  3  2  1  
*************************compare(test2)****************************
1
*************************pop_back()****************************
c  c  b  1  2  3  4  5  a  a  a  a  a  b  c  d  e  b  c  d  l  m  n  l  m  n  o  p  q  a  b  3  2  1  a  a  b  c  d  e  a  a  a  a  a  3  2  
*************************test.swap(test2)****************************
a  b  c  d  e  
*************************clear()****************************

6. Other operations

Substring and other operations

Keywords: C++

Added by kendallkamikaze on Sun, 19 Sep 2021 01:31:18 +0300