C++ STL总结(四)list

1.list内存结构

list是双向链表,元素在内存中并不是连续存储的。它在每次插入和删除一个元素时都会申请和释放一个元素的空间。
在这里插入图片描述

2.list容量操作

函数功能
bool empty()检查容器是否为空
size_t size()返回容器中元素的个数
size_t max_size()返回容器由于系统或库的实现所限制可容纳的元素最大数量
void resize(size_t new_size, const T& x)重设容器的size大小,如果size变大,则以默认值填充变长的位置,如果size变小,则删除变短那部分的尾部元素
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;

}

结果:

*************************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构造

函数功能
list()默认构造函数,构造一个空容器
list(size_t n)构造具有n个默认值的容器
list(size_t n, const T& value)构造具有n个值为value的容器
list(first, last)构造拥有范围 [first, last) 内容的容器
list( const list& other )拷贝构造函数,构造一个拥有other内容的容器
list( list&& other )移动构造函数,用移动语义构造拥有other内容的容器,移动后other为empty()
list( std::initializer_list init)构造拥有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元素访问

函数功能
front()访问第一个元素
back()访问最后一个元素
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元素修改

函数功能
push_back(const T& value)在尾部插入新元素value
push_front(const T& value)在头部插入新元素value
insert(iterator pos, const T& value)在pos前插入元素value
insert(iterator pos, size_t n, const T& value)在pos前插入n个元素value
insert(iterator pos, iterator first, iterator last)在pos前插入来自范围 [first, last)的元素
erase(iterator pos)移除pos处的元素
erase(iterator first, iterator last)移除范围 [first, last) 的元素
emplace_back(Args&&… args)在容器尾部就地构造元素
emplace_front(Args&&… args)在容器头部就地构造元素
pop_back()删除尾部元素
pop_front()删除头部元素
swap(list& other)将内容与other交换。不在单个元素上调用任何移动、复制或交换操作
clear()清除所有元素
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操作

函数功能
merge(list& other)将两个已排序的链表合并为一个排序链表
splice(const iterator pos, list& other)从other转移所有元素到本list,不复制或移动元素
splice(const iterator pos, list& other, const iterator it)从other转移it指向的元素到本list,不复制或移动元素
splice(const iterator pos, list& other, const iterator first, const iterator last)从other转移范围[first,last)的元素到本list,不复制或移动元素
reverse()逆转容器中的元素顺序
unique()移除元素中相继重复的元素
sort()元素排序
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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值