Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c++list

作者:看悲伤的曲   发布日期:2026-03-17   浏览:52

#include <iostream>
#include <list>

int main() {
    // 创建一个 list 容器,存储 int 类型的数据
    std::list<int> myList;

    // 向 list 中添加元素
    myList.push_back(10);  // 在 list 的末尾添加元素 10
    myList.push_back(20);  // 在 list 的末尾添加元素 20
    myList.push_front(5);  // 在 list 的开头添加元素 5

    // 遍历并打印 list 中的元素
    for (std::list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 删除 list 中的第一个元素
    myList.pop_front();

    // 删除 list 中的最后一个元素
    myList.pop_back();

    // 再次遍历并打印 list 中的元素
    for (std::list<int>::iterator it = myList.begin(); it != myList.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    return 0;
}

解释说明:

  1. 包含头文件#include <list> 引入了 C++ 标准库中的 list 容器。
  2. 创建 list 容器std::list<int> myList; 创建了一个存储 int 类型数据的 list 容器。
  3. 添加元素
    • push_back() 方法在 list 的末尾添加元素。
    • push_front() 方法在 list 的开头添加元素。
  4. 遍历和打印:使用迭代器(iterator)遍历 list 中的元素,并通过 *it 访问每个元素。
  5. 删除元素
    • pop_front() 方法删除 list 中的第一个元素。
    • pop_back() 方法删除 list 中的最后一个元素。
  6. 再次遍历和打印:删除元素后,再次遍历并打印 list 中剩余的元素。

上一篇:c++创建线程

下一篇:c++ strstr

大家都在看

c++闭包

c++向上取整的代码

c++单引号和双引号的区别

c++ 注释

c++如何判断素数

c++ functional

c++框架代码

c++格式化字符串

c++ orm框架

c++ string类

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站