当前位置: 首页 > article >正文

c++ list

1.构造函数

构造函数

// list<T> lst;
// list(beg, end); // 区间构造
// list(n, elem); // 元素构造   
// list(const list &lst); // 拷贝构造
#include <iostream>
#include <fstream>
#include <string>
#include <list>
using namespace std;

void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}


void test01() {
    list<int> l; // 默认构造
    l.push_back(10);
    l.push_back(20);
    l.push_back(30);
    l.push_back(40);
    printList(l);

    // 区间构造
    list<int> l2(l.begin(), l.end());
    printList(l2);

    // 拷贝构造
    list<int> l3(l2);
    printList(l3);

    // 元素构造
    list<int> l4(10, 100);
    printList(l4);

}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}


2 赋值和交换

// 函数原型:
// assign(beg, end) // 将区间[beg, end)中的数据拷贝到本身
// assign(n, elem) // 将n个elem拷贝赋值给本身
// list &operator=(const list &L); // 重载=运算符
// swap(L) // 将L与本身的元素互换

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;


void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}


void test01() {
    list<int> l; // 默认构造
    l.push_back(10);
    l.push_back(20);
    l.push_back(30);
    l.push_back(40);
    printList(l);

    // assign(beg, end) // 将区间[beg, end)中的数据拷贝到本身
    list<int> l2;
    l2.assign(l.begin(), --l.end() );
    printList(l2);
    
    // assign(n, elem) // 将n个elem拷贝赋值给本身
    l2.assign(5, 100);
    cout << "L2 " << endl;
    printList(l2);

    // swap 
    l2.swap(l);
    cout << "l 的值" << endl;
    printList(l);
    cout << "l2的值" << endl;
    printList(l2);

}

int main(int argc, char const *argv[]) {
    

    test01();
    return 0;
}


3 list 大小操作

函数原型:

size() // 返回容器中元素的个数
empty() // 判断容器是否为空
resize(int num) // 重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(int num, elem) // 重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

//   list 大小操作

void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}


void test01() {
    list<int> l; // 默认构造
    l.push_back(10);
    l.push_back(20);
    l.push_back(30);
    l.push_back(40);
    printList(l);

    if(l.empty()) {
        cout << "l is empty" << endl;
    }
    else{
        cout << "l is not empty" << endl;
    }
    cout << "l size = " << l.size() << endl;

    l.resize(10);
    printList(l);
    cout << "l size = " << l.size() << endl;

    l.resize(2);
    printList(l);
    cout << "l size = " << l.size() << endl;
    
    l.resize(10, 100);
    printList(l);
    

}

int main(int argc, char const *argv[]) {
    

    test01();
    return 0;
}


4 list 插入和删除

函数原型
puhsh_back(elem) 在容器尾部插入元素
pop_back() 删除容器最后一个元素
push_front(elem) 在容器头部插入元素
pop_front() 删除容器第一个元素
insert(pos, elem) // 在pos位置插入元素elem,返回新的数据位置
insert(pos,n,elem) // 在pos位置插入n个elem,无返回值
insert(pos,beg,end) // 在pos位置插入[beg,end)区间的数据,无返回值
clear() // 清空容器数据
erase(pos) // 删除pos位置的数据, 返回删除的元素的下一个位置
erase(beg,end) // 删除[beg,end)区间的数据,返回删除的元素的下一个位置
remove(value) // 删除容器中所有与value值匹配的元素
clear() // 清空容器数据

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}


void test01() {
    list<int> L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    // 头插
    L1.push_front(100);
    L1.push_front(200);
    L1.push_front(300);
    // 300 200 100 10 20 30
    printList(L1);

    // 尾删
    L1.pop_back();
    // 300 200 100 10 20
    printList(L1);
    // 头删
    L1.pop_front();
    // 200 100 10 20
    printList(L1);

    // insert 插入
    list<int>::iterator it = L1.begin();
    L1.insert(++it, 1000);
    printList(L1);
    
    // 删除
    it = L1.begin();
    L1.erase(++it);
    printList(L1);

    // remove 删除所有值为777的元素
    L1.push_back(777);
    L1.push_back(777);
    L1.push_back(777);
    printList(L1);
    L1.remove(777);
    printList(L1);

    // 清空
    L1.clear();
    printList(L1);

}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}


5. list数据存取

函数原型

// front() 返回第一个元素的引用
// back() 返回最后一个元素的引用
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

//   list数据存取

// 函数原型
// front() 返回第一个元素的引用
// back() 返回最后一个元素的引用

// 


void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}


void test01() {
    list<int> L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    printList(L1);

    // L1[0] 不可以访问
    // L1.at(0) 不可以访问
    // 原因是list本质是链表,不是连续线性空间存储数据,迭代器也不支持随机访问
    cout << "front: " << L1.front() << endl;

    cout << "back: " << L1.back() << endl;

    // 验证迭代器不支持随机访问
    list<int>::iterator it = L1.begin();
    it++;
    it--;
    // 不可以进行it+1 // 不支持随机访问

}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}


6. list 反转和排序

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

//  list 反转和排序

// 函数原型
// sort() 返回第一个元素的引用
// reverse() 反转链表


void printList(const list<int> &L){
    for(list<int>::const_iterator it = L.begin(); it != L.end(); it++){
        cout << *it << " ";
    }
    cout << endl;
}

bool myCompare(int v1, int v2){
    // 当前元素比参数大,则返回true
    return v1 > v2;
}

void test01() {
    list<int> L1;
    L1.push_back(10);
    L1.push_back(50);
    L1.push_back(30);
    printList(L1);

    // 反转
    L1.reverse();
    printList(L1);

    // 排序
    L1.sort(); // 默认从小到大
    printList(L1);
    L1.sort(myCompare); // 从大到小
    printList(L1);

    // 所有不支持随即访问的容器,不支持标准的sort算法
    // sort(L1.begin(), L1.end()); // 运行时报错

}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}


7. 排序案例

案例描述: 将person自定义数据类型进行排序,Person中属性有 姓名 年龄 身高
排序规制: 按照年龄进行升序

#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;

//  list 排序案例
// 案例描述: 将person自定义数据类型进行排序,Person中属性有 姓名 年龄 身高
// 排序规制: 按照年龄进行升序


class Person{
public:
    Person(string name, int age, int height) {
        this->m_Age = age;
        this->m_Height = height;
        this->m_Name = name;
    }

    string m_Name;
    int m_Age;
    int m_Height;
};

void printList (list<Person> &L) {
    for(list<Person>::iterator it = L.begin(); it != L.end(); it++){
        cout << "姓名: " << it->m_Name << " 年龄: " << it->m_Age << " 身高: " << it->m_Height << endl;
    }
    cout << endl;
}

bool comparePerson(Person &p1, Person &p2) {
    if(p1.m_Age == p2.m_Age) {
        // 年龄相同 按照身高降序
        return p1.m_Height > p2.m_Height;
    }
    // 年龄升序
    return p1.m_Age < p2.m_Age;
}

void test01() {
    list<Person> L;
    L.push_back(Person("刘备", 35, 180));
    L.push_back(Person("关羽", 35, 175));
    L.push_back(Person("赵云", 26, 160));
    L.push_back(Person("曹操", 45, 190));
    L.push_back(Person("张飞", 35, 170));
    L.push_back(Person("诸葛亮", 28, 175));

    //插入数据
    printList(L);

    //排序后
    L.sort(comparePerson);
    printList(L);

}

int main(int argc, char const *argv[]) {
    test01();
    return 0;
}



http://www.kler.cn/a/521359.html

相关文章:

  • 一次端口监听正常,tcpdump无法监听到指定端口报文问题分析
  • 以用户为中心,优化 B 端界面设计
  • this、self、window、top 在 JavaScript 中的区别深入研究
  • 996引擎 - NPC-动态创建NPC
  • “AI质量评估系统:智能守护,让品质无忧
  • 人格分裂(交互问答)-小白想懂Elasticsearch
  • 1.26寒假作业
  • ray.rllib-入门实践-10:自定义环境
  • 【Elasticsearch】聚合分析:管道聚合
  • 基于 Arduino Uno 和 RFID-RC522 的 RFID 卡号读取技术详解
  • 深度学习模型架构演进:从RNN到新兴技术
  • 数据结构——查找算法和排序算法
  • 【C++】std::prev用法
  • ubuntu下编译openjdk17,依赖的包名有所不同
  • 基于 RAMS 的数据驱动建模与应用实践:从理论到具体操作
  • 1.26 实现文件拷贝的功能
  • 我的2024年年度总结
  • 自然元素有哪些选择?
  • K8S部署DevOps自动化运维平台
  • Arouter详解・常见面试题