c++基础:8.2.2 _STL容器-list

本文详细介绍了C++ STL容器List的基础操作,包括遍历、添加删除元素、任意位置插入删除、特有的函数如reverse和sort。还讨论了面试中常见的List与Vector之间的转化,并分享了实现miniSTL中List的部分代码,涵盖了迭代器、运算符重载和通用算法的应用。
摘要由CSDN通过智能技术生成

(***)1 基础操作

1.1 遍历+正反顺序打印

(1)C++11遍历的方法

for(auto n:l){
    // (1)C++11遍历的方法
        cout << n << " ";
    }
    cout << endl;

(2)不支持下标访问

for(int i=0;i<l.size();++i){
   
        // cout << l[i] << " "; // (2)不支持下标访问
    }

(3)正序打印

list<int>::iterator it = l.begin();  //(3)正序打印
    while(it != l.end()){
   
       cout << *it << " ";
       ++it;
    }
    cout << endl;

(4)逆序打印

list<int>::reverse_iterator rit = l.rbegin();//(4)逆序打印
    while(rit != l.rend()){
   
       cout << *rit << " ";
       ++rit;
    }
    cout << endl;

(完整代码 001_list_Travel.cpp)

#include <iostream>
#include <list>
#include <vector>

using namespace std;
void Travesal(const list<int>& l){
   
    list<int>::const_iterator it = l.cbegin();//cbegin() C++11
    while(it != l.cend()){
    //cend() C++11
       cout << *it << " ";
       ++it;
    }
    cout << endl;
}
void TravesalReverse(const list<int>& l){
   
    list<int>::const_reverse_iterator rit = l.crbegin();
    while(rit != l.crend()){
   
       cout << *rit << " ";
       ++rit;
    }
    cout << endl;
}
int main()
{
   
     list<int> l = {
   1,2,3,4,5};// C++11

    for(auto n:l){
    // (1)C++11遍历的方法
        cout << n << " ";
    }
    cout << endl;

    for(int i=0;i<l.size();++i){
   
        // cout << l[i] << " "; // (2)不支持下标访问
    }

    list<int>::iterator it = l.begin();  //(3)正序打印
    while(it != l.end()){
   
       cout << *it << " ";
       ++it;
    }
    cout << endl;

    list<int>::reverse_iterator rit = l.rbegin();//(4)逆序打印
    while(rit != l.rend()){
   
       cout << *rit << " ";
       ++rit;
    }
    cout << endl;
    Travesal(l); 
    TravesalReverse(l);

}

1.2 添加,删除

(1)尾添加

 l.push_back(6);

(2)尾删除

l.pop_back();

(3) 首添加

l.push_front(-2);

(4) 首删除

l.pop_front();

(源码见002_push_pop.cpp)

#include <iostream>
#include <list>
#include <vector>

using namespace std;
void Travesal(const list<int>& l){
   
    list<int>::const_iterator it = l.cbegin();//cbegin() C++11
    while(it != l.cend()){
    //cend() C++11
       cout << *it << " ";
       ++it;
    }
    cout << endl;
}
void TravesalReverse(const list<int>& l){
   
    list<int>::const_reverse_iterator rit = l.crbegin();
    while(rit != l.crend()){
   
       cout << *rit << " ";
       ++rit;
    }
    cout << endl;
}
int main()
{
   
     list<int> l = {
   1,2,3,4,5};// C++11
     Travesal(l);

     // 尾添加
    l.push_back(6);
    l.push_back(7);
    l.push_back(8);
    Travesal(l);
    // 尾删除
    l.pop_back();
    l.pop_back();
    l.pop_back();
    Travesal(l);

    // 首添加
    l.push_front(-2);
    l.push_front(-1);
    l.push_front(0);
    Travesal(l);
    // 首删除
    l.pop_front();
    l.pop_front();
    l.pop_front();
    Travesal(l);

}

1.3 任意位置添加、删除

(1)insert():list的迭代器不能做算术运算,没有算术运算符重载,但是可以自增自减(左闭又开的区间)

// 插入
    auto it = l.begin();
    ++it;
    // it = it + 1; // (1)list的迭代器不能做算术运算,没有算术运算符重载,但是可以自增自减
    l.insert(it,100); //表示在begin()+1的内存插入100,内存发生改变;此时it指针指向100的后面;(左闭又开的区间)

(2)erase():想要删除100,必须指向100的内存;必须先自减

 // 删除
    --it;
    l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值