笔记(五)——list容器的基础理论知识

list容器是一个双向链表容器,可以高效地进行插入删除元素,但是不能随机存取元素(不支持at()和[]操作符)。

一、list容器的对象构造方法

list对象采用模板类的默认构造形式

例如list<T> lst;

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int> lstInt;
    list<float> lstFloat;
    list<string> listString;
    list<int>::iterator t1;
    list<int>::iterator t2;
        
    lstInt.assign(arr,arr+5);
    lstInt.push_back(5);//在容器尾部插入元素
    lstInt.push_back(5);//在容器尾部删除元素
    lstInt.pop_back();
    lstInt.push_front(0);//在容器头部插入元素
    lstInt.push_front(0);
    lstInt.pop_front();//在容器头部删除元素
    t1=lstInt.begin(); 
    t2=lstInt.end(); 
    
//    正确写法 
    for(;t1!=t2;t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    
    //    错误写法 
//    for(;t1<t2;t1++)
//    {
//        cout<<*t1;
//    }
//    cout<<endl;

//输出:0012345 
    
    return 0;
}

list对象的带参构造方式

  1. list<T> lst(beg,end);该构造函数将区间[beg,end)中的元素拷贝给本身。

beg,end是数组元素的地址。

  1. list<T> list(n,elem);该构造函数将n个elem拷贝给本身。

  1. list<T> lst1(lst2);拷贝构造函数

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int>::iterator t;
 
//1、list对象带参数构造 
//正确写法 
    list<int> lst1(arr,arr+5);                              //建立一个存放int的list容器,初始为0,1,2,3,4 
    list<int> lst2(lst1.begin(),lst1.end());           //建立一个存放int的list容器,初始为0,1,2,3,4 
    list<int> lst3(3,100);       
    list<int> lst4(lst1); 
         
    for(t=lst1.begin();t!=lst1.end();t++)
    {
        cout<<*t<<" ";
    }
    
    cout<<endl;
    for(t=lst2.begin();t!=lst2.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;
    
    for(t=lst3.begin();t!=lst3.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;
    
    for(t=lst4.begin();t!=lst4.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;

//输出
//0 1 2 3 4
//0 1 2 3 4
//100 100 100
//0 1 2 3 4 
    return 0;
} 

二、list与迭代器

list容器的迭代器是双向迭代器。

  1. list.begin();返回容器第一个元素的迭代器。

  1. list.end();返回容器最后一个元素之后的迭代器。

  1. list.rbegin();返回容器倒数第一个元素的迭代器。

  1. list.rend();返回容器倒数最后一个元素后面的迭代器。

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int> lstInt;
    list<int>::iterator t1;
    list<int>::iterator t2;
        
    lstInt.assign(arr,arr+5);
    t1=lstInt.begin(); 
    t2=lstInt.end(); 
    
//    正确写法 
    for(;t1!=t2;t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    
    //    错误写法 
//    for(;t1<t2;t1++)
//    {
//        cout<<*t1;
//    }
//    cout<<endl;

//输出:01234
    
    return 0;
}

三、list容器的赋值

1、list.assign(beg,end); 将区间[beg,end)中的元素拷贝给本身。

2、list.assign(n,elem);将n个elem拷贝给本身。

3、list& operator=(const list &vec);重载等号操作符。

4、list.swap(vec);将vec与本身的元素交换。

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int>::iterator t;
 
    list<int> lst1;              
    list<int> lst2;           
    list<int> lst3;       
    
    lst1.assign(arr,arr+5);
    lst2.assign(3,100);
    lst3=lst1;
    lst2.swap(lst1);
    
         
    for(t=lst1.begin();t!=lst1.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;
    
    for(t=lst2.begin();t!=lst2.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;
    
    for(t=lst2.begin();t!=lst2.end();t++)
    {
        cout<<*t<<" ";
    }
    cout<<endl;
//输出:
//100 100 100
//0 1 2 3 4
//0 1 2 3 4 
return ; 
} 

四、list容器的大小

list.size();返回容器中元素的个数

list.empty();判断容器是否为空

list.resize(num);重新指定容器长度,若比之前的长度长,超出部分填充默认值,若比之前的长度短,删除超出部分元素。

list.resize(num,elem);重新指定容器长度,若比之前的长度长,超出部分填充指定值,若比之前的长度短,删除超出部分元素。

五、list容器元素的插入

list.insert(pos,elem);在pos位置插入一个elem元素,返回新元素的位置(迭代器类型)

list.insert(pos,n, elem);在pos位置插入n个elem元素,无返回值

list.insert(pos,beg, end);在pos位置插入[beg,end)区间的数据,无返回值。

六、list容器的删除

1、list.clear();移除容器的所有数据

2、list.erase(beg,end);删除[beg,end)区间的数据,返回下一个数据的位置。

3、list.erase(pos);删除pos位置的元素,返回下一个数据的位置。

4、list.remove(elem);删除容器里所有值为elem的元素。

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int> lstInt;
    list<int>::iterator t1;
    list<int>::iterator t2;
    list<int>::iterator t3;
        
    lstInt.assign(arr,arr+5);
    t1=lstInt.begin(); 
    
    for(;t1!=lstInt.end();t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    //01234
    
    lstInt.clear();
    lstInt.push_front(5);
    lstInt.push_front(6);
    lstInt.push_front(7);
    lstInt.push_front(8);
    for(t1=lstInt.begin();t1!=lstInt.end();t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    //8765
    
    t2=++lstInt.begin();
    t3=++lstInt.begin();
    ++t3;
    ++t3;
    lstInt.erase(t2,t3);
    for(t1=lstInt.begin();t1!=lstInt.end();t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    //85
    
    lstInt.push_front(5);
    lstInt.push_front(6);
    lstInt.push_front(7);
    lstInt.push_front(8);
    lstInt.remove(5);
    for(t1=lstInt.begin();t1!=lstInt.end();t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
    //8768
    
//输出
//01234
//8765
//85
//8768
        
        
    return 0;
}

七、其他

  1. lst.reverse();反转列表

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4};
    list<int> lstInt;
    list<int>::iterator t1;
    list<int>::iterator t2;
        
    lstInt.assign(arr,arr+5);
    lstInt.reverse();
    t1=lstInt.begin(); 
    t2=lstInt.end(); 
    

    for(;t1!=t2;t1++)
    {
        cout<<*t1;
    }
    cout<<endl;
//    输出:43210 
    return 0;
}

2、删除结点导致迭代器失效

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int arr[]={0,1,2,3,4,4,4,4,4,4,4,5,5,6,6};
    list<int> lstInt;
    list<int>::iterator t1;
    list<int>::iterator t2;
        
    lstInt.assign(arr,arr+15);
    t1=lstInt.begin(); 
    t2=lstInt.end(); 
    
//因为list容器使用不连续分配的内存,并且它的erase方法会返回下一个有效的迭代器,所有遍历删除结点可以有以下方式: 
//方法1 
    for(;t1!=t2;)
    {
        if(*t1==4)
        {
            t1=lstInt.erase(t1);
        }
        else
        {
            t1++;
        }
    }
    
//方法2
    for(;t1!=t2;t1++)
    {
        if(*t1==4)
        {
            lstInt.erase(t1);
        }
    }
    
    t1=lstInt.begin(); 
    t2=lstInt.end(); 
    for(;t1!=t2;t1++)
    {
        cout<<*t1<<" ";
    }
    cout<<endl;
//输出:0 1 2 3 5 5 6 6 
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!感谢提问。下面是有关 K210 学习笔记中串口通信的一些内容: 在 K210 开发板上,串口是一种常见的通信接口,用于将数据从一个设备传输到另一个设备。串口通信通常使用 UART (Universal Asynchronous Receiver/Transmitter) 协议。 要在 K210 上进行串口通信,首先需要使用两个引脚:一个用于接收数据(RX),一个用于发送数据(TX)。这些引脚可以通过 GPIO 端口与外部设备连接。 在 K210 的开发环境中,可以使用 MaixPy 或者 Kendryte Standalone SDK 进行串口编程。 在 MaixPy 中,可以使用 `uart` 模块来进行串口通信。例如,要初始化一个串口对象并设置波特率为 115200,可以使用以下代码: ```python from machine import UART uart = UART(UART.UART1, 115200) ``` 然后,可以使用 `uart.write()` 函数发送数据,使用 `uart.read()` 函数接收数据。例如: ```python uart.write("Hello, world!\n") data = uart.read(10) ``` 在 Kendryte Standalone SDK 中,可以使用 `uart.h` 头文件中的函数来进行串口通信。例如,要初始化一个串口对象并设置波特率为 115200,可以使用以下代码: ```c #include "uart.h" uart_init(UART_DEVICE_1, 115200); ``` 然后,可以使用 `uart_send_data()` 函数发送数据,使用 `uart_receive_data()` 函数接收数据。例如: ```c uart_send_data(UART_DEVICE_1, "Hello, world!\n", 14); char buffer[10]; uart_receive_data(UART_DEVICE_1, buffer, 10); ``` 以上是关于 K210 学习笔记中串口通信的简要介绍。如果你有更具体的问题,请随时提问!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值