C++ STL summary list

Posted by upit on Wed, 15 Sep 2021 02:21:09 +0200

1.list memory structure

List is a two-way linked list, and the elements are not stored continuously in memory. It will request and release the space of an element every time it inserts and deletes an element.

2. Capacity operation

functionfunction
bool empty()Check whether the container is empty
size_t size()Returns the number of elements in the container
size_t max_size()Returns the maximum number of elements that the container can hold due to the implementation of the system or library
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 test_capacity(){
        list<int> test;
        cout << "*************************after init****************************\n";
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << endl;

        cout << "*************************after push_back****************************\n";
        test.push_back(1);
        cout << "empty(): " << test.empty() << "  size(): " << test.size() << "  max_size(): " << test.max_size() << endl;

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

}

result:

*************************after init****************************
empty(): 1  size(): 0  max_size(): 384307168202282325
*************************after push_back****************************
empty(): 0  size(): 1  max_size(): 384307168202282325
*************************after resize(5)****************************
empty(): 0  size(): 5  max_size(): 384307168202282325

3.list structure

functionfunction
list()The default constructor constructs an empty container
list(size_t n)Construct a container with n default values
list(size_t n, const T& value)Construct a container with n values of value
list(first, last)Construct a container with the contents of the range [first, last]
list( const list& other )Copy constructor to construct a container with other content
list( list&& other )The mobile constructor constructs a container with other content with mobile semantics. After moving, other is empty()
list( std::initializer_list init)Construct a container with the contents of initializer_list init
void test_construct(){
        cout << "*************************list()****************************\n";
        list<int> test;
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************list(10)****************************\n";
        list<int> test1(10);
        for(auto iter=test1.cbegin(); iter!=test1.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************list(10,2)****************************\n";
        list<int> test2(10,2);
        for(auto iter=test2.cbegin(); iter!=test2.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout  << endl;

        cout << "*************************list(test2.begin(), test2.end())****************************\n";
        list<int> test3(test2.begin(), test2.end());
        for(auto iter=test3.cbegin(); iter!=test3.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************list(test2)****************************\n";
        list<int> test4(test2);
        for(auto iter=test4.cbegin(); iter!=test4.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout <<endl;
        
        cout << "*************************list(std::move(test2))****************************\n";
        list<int> test5(std::move(test2));
        for(auto iter=test5.cbegin(); iter!=test5.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout <<endl;

        cout << "*************************list{1,2,3,4,5}****************************\n";
        list<int> test6{1,2,3,4,5};
        for(auto iter=test6.cbegin(); iter!=test6.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;
}


*************************list()****************************

*************************list(10)****************************
0  0  0  0  0  0  0  0  0  0  
*************************list(10,2)****************************
2  2  2  2  2  2  2  2  2  2  
*************************list(test2.begin(), test2.end())****************************
2  2  2  2  2  2  2  2  2  2  
*************************list(test2)****************************
2  2  2  2  2  2  2  2  2  2  
*************************list(std::move(test2))****************************
2  2  2  2  2  2  2  2  2  2  
*************************list{1,2,3,4,5}****************************
1  2  3  4  5  

4.list element access

functionfunction
front()Access the first element
back()Access the last element
void test_access(){
        list<int>test{1,2,3,4,5};
        cout<< "  front:" << test.front() << "  back:" << test.back() << endl;
}
 front:1  back:5

5.list element modification

functionfunction
push_back(const T& value)Insert a new element value at the end
push_front(const T& value)Insert a new element value in the header
insert(iterator pos, const T& value)Insert element value before pos
insert(iterator pos, size_t n, const T& value)Insert n elements value before pos
insert(iterator pos, iterator first, iterator last)Insert an element from the range [first, last] before the pos
erase(iterator pos)Remove element at pos
erase(iterator first, iterator last)Remove elements of the range [first, last]
emplace_back(Args&&... args)Construct elements in place at the end of the container
emplace_front(Args&&... args)Construct elements in place at the head of the container
pop_back()Delete tail element
pop_front()Delete header element
swap(list& other)Swap content with other. No move, copy, or swap operations are called on a single element
clear()Clear all elements
void test_modifier(){
        list<int>test;
        cout << "*************************push_back(1)****************************\n";
        test.push_back(1);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************push_front(2)****************************\n";
        test.push_back(2);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),2)****************************\n";
        test.insert(test.begin(), 2);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),3,4)****************************\n";
        test.insert(test.begin(), 3, 4);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************insert(test.begin(),test2.begin(),test2.end())****************************\n";
        list<int>test2{1,2,3,4,5};
        test.insert(test.begin(), test2.begin(), test2.end());
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin())****************************\n";
        test.erase(test.begin());
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************erase(test.begin(),test.end())****************************\n";
        test.erase(test.begin(), test.end());
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;


        cout << "*************************emplace_back****************************\n";
        class Book{
        public:
                Book(const std::string& name):m_name(name){}
                inline std::string getName(){return m_name;}
        private:
                std::string m_name;
        };
        list<Book> test_book;
        test_book.emplace_back("C++ Primer");
        for(auto iter=test_book.begin(); iter!=test_book.end(); ++iter){
                cout << iter->getName() << "  ";
        }
        cout << endl;

        cout << "*************************emplace_front****************************\n";
        test_book.emplace_front("Effective C++");
        for(auto iter=test_book.begin(); iter!=test_book.end(); ++iter){
                cout << iter->getName() << "  ";
        }
        cout << endl;

        cout << "*************************pop_back()****************************\n";
        for(int i=0;i<5;++i){
                test.push_back(i);
        }
       test.pop_back();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************pop_front()****************************\n";
        test.pop_front();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************test.swap(test2)****************************\n";
        test.swap(test2);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************clear()****************************\n";
        test.clear();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

*************************push_back(1)****************************
1  
*************************push_front(2)****************************
1  2  
*************************insert(test.begin(),2)****************************
2  1  2  
*************************insert(test.begin(),3,4)****************************
4  4  4  2  1  2  
*************************insert(test.begin(),test2.begin(),test2.end())****************************
1  2  3  4  5  4  4  4  2  1  2  
*************************erase(test.begin())****************************
2  3  4  5  4  4  4  2  1  2  
*************************erase(test.begin(),test.end())****************************

*************************emplace_back****************************
C++ Primer  
*************************emplace_front****************************
Effective C++  C++ Primer  
*************************pop_back()****************************
0  1  2  3  
*************************pop_front()****************************
1  2  3  
*************************test.swap(test2)****************************
1  2  3  4  5  
*************************clear()****************************

6.list operation

functionfunction
merge(list& other)Merge two sorted linked lists into one sorted linked list
splice(const iterator pos, list& other)Transfer all elements from other to this list without copying or moving elements
splice(const iterator pos, list& other´╝î const iterator it)Transfer the element pointed to by it from other to this list without copying or moving the element
splice(const iterator pos, list& other, const iterator first, const iterator last)Transfer the elements of the range [first,last) from other to this list without copying or moving the elements
reverse()Reverse the order of elements in the container
unique()Remove successive repeating elements from the element
sort()Element sorting
void test_operation(){
        cout << "*************************merge()****************************\n";
        list<int> test{1,3,5,7};
        list<int> test2{2,3,4,5,8};
        test.merge(test2);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************splice(test.begin(),test3)****************************\n";
        list<int> test3{7,8,9};
        test.splice(test.begin(), test3);
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************splice(test.begin(),test4, test4.begin())****************************\n";
        list<int> test4{7,8,9};
        test.splice(test.begin(), test4, test4.begin());
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************splice(test.begin(),test4,test4.begin(),test4.end())****************************\n";
        test.splice(test.begin(), test4, test4.begin(), test4.end());
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************reverse()****************************\n";
        test.reverse();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************unique()****************************\n";
        test.unique();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

        cout << "*************************sort()****************************\n";
        test.sort();
        for(auto iter=test.cbegin(); iter!=test.cend(); ++iter){
                cout << *iter << "  ";
        }
        cout << endl;

*************************merge()****************************
1  2  3  3  4  5  5  7  8  
*************************splice(test.begin(),test3)****************************
7  8  9  1  2  3  3  4  5  5  7  8  
*************************splice(test.begin(),test4, test4.begin())****************************
7  7  8  9  1  2  3  3  4  5  5  7  8  
*************************splice(test.begin(),test4,test4.begin(),test4.end())****************************
8  9  7  7  8  9  1  2  3  3  4  5  5  7  8  
*************************reverse()****************************
8  7  5  5  4  3  3  2  1  9  8  7  7  9  8  
*************************unique()****************************
8  7  5  4  3  2  1  9  8  7  9  8  
*************************sort()****************************
1  2  3  4  5  7  7  8  8  8  9  9 

Topics: C++ list