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

百度网站地图怎么做外链推广是什么意思

百度网站地图怎么做,外链推广是什么意思,手表品牌,全网营销型推广网站建设摘要 C 标准库中的 std::list 是一种双向链表容器&#xff0c;它允许在常数时间内进行插入和删除操作&#xff0c;每个元素包含一个指向前一个和后一个元素的指针。这给我们开发提供了高效的插入和删除操作。 引入头文件 要使用 std::list&#xff0c;需要包含头文件 <li…

摘要

C++ 标准库中的 `std::list` 是一种双向链表容器,它允许在常数时间内进行插入和删除操作,每个元素包含一个指向前一个和后一个元素的指针。这给我们开发提供了高效的插入和删除操作。

引入头文件

要使用 `std::list`,需要包含头文件 `<list>`:

#include <list>

创建和初始化

#include <iostream>
#include <list>int main() {std::list<int> l1;                      // 默认构造函数std::list<int> l2(5, 10);               // 创建包含 5 个值为 10 的元素的列表std::list<int> l3 = {1, 2, 3, 4, 5, 6}; // 列表初始化// 输出列表 l3 的内容for (int n : l3) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

常用方法和操作

1. 插入和删除元素

#include <iostream>
#include <list>int main() {std::list<int> l = {1, 2, 3, 4, 5, 6};// 在末尾添加元素l.push_back(6);// 在开头添加元素l.push_front(0);// 在第三个位置插入元素auto it = l.begin();std::advance(it, 3); // 前进 3 个位置l.insert(it, 99);// 删除开头的元素l.pop_front();// 删除末尾的元素l.pop_back();// 删除特定位置的元素it = l.begin();std::advance(it, 2); // 前进 2 个位置l.erase(it);// 输出列表 l 的内容for (int n : l) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

2. 访问元素

`std::list` 不支持随机访问(即不支持 `operator[]`),但是可以使用迭代器遍历和访问元素:

#include <iostream>
#include <list>int main() {std::list<int> l = {1, 2, 3, 4, 5, 6};// 使用迭代器遍历for (auto it = l.begin(); it != l.end(); ++it) {std::cout << *it << ' ';}std::cout << std::endl;// 使用范围 for 循环遍历for (int n : l) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

3. 反向遍历

#include <iostream>
#include <list>int main() {std::list<int> l = {1, 2, 3, 4, 5, 6};// 反向遍历for (auto it = l.rbegin(); it != l.rend(); ++it) {std::cout << *it << ' ';}std::cout << std::endl;return 0;
}

4. 其它用法

#include <iostream>
#include <list>int main() {std::list<int> l = {1, 2, 3, 4, 5, 6};// 获取列表大小std::cout << "Size: " << l.size() << std::endl;// 检查是否为空if (l.empty()) {std::cout << "List is empty" << std::endl;} else {std::cout << "List is not empty" << std::endl;}// 清空列表l.clear();std::cout << "Size after clear: " << l.size() << std::endl;return 0;
}

进阶使用技巧

1. 合并和排序

#include <iostream>
#include <list>int main() {std::list<int> l1 = {1, 3, 5, 7};std::list<int> l2 = {2, 4, 6, 8};// 合并两个已排序的列表l1.merge(l2);// 排序l1.sort();// 反转l1.reverse();// 输出列表 l1 的内容for (int n : l1) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

2. 去重

#include <iostream>
#include <list>int main() {std::list<int> l = {1, 2, 2, 3, 3, 3, 4, 5};// 必须先排序,然后去重l.sort();l.unique();// 输出去重后的列表 l 的内容for (int n : l) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

自定义比较函数

在 `sort`、`merge` 等方法中,可以传递自定义比较函数:

#include <iostream>
#include <list>bool customCompare(int a, int b) {return a > b; // 降序排列
}int main() {std::list<int> l = {1, 3, 2, 5, 4};// 使用自定义比较函数排序l.sort(customCompare);// 输出排序后的列表 l 的内容for (int n : l) {std::cout << n << ' ';}std::cout << std::endl;return 0;
}

总结

`std::list` 是 C++ 标准库中功能强大且灵活的双向链表容器,适用于需要频繁插入和删除操作的场景。在我们实际开发中,根据项目的具体需求选择合适的容器,比如‘std::forward_list’等,可以显著提高代码性能和可维护性。

引用

std::list - cppreference.com

http://www.rdtb.cn/news/11799.html

相关文章:

  • 网站备案需要关站网络事件营销案例
  • 青岛做视频的网站香飘飘奶茶
  • dreamweaver做的网站windows优化大师卸载不掉
  • 滨州做网站建设第三方网络营销平台有哪些
  • 网站设计中新闻版块怎么做抖音seo怎么做
  • 网站制作公司 北京上海网络推广营销策划方案
  • 做to b的网站昆明百度搜索排名优化
  • 服务类的网站怎么做公众号软文素材
  • 网站怎么做用户体验网络推广和网站推广平台
  • 镇江百度公司站内关键词自然排名优化
  • 洗浴按摩这个词可以做网站不域名注册优惠
  • 建设的网站网站推广主要是做什么
  • 可不可以免费创建网站网页设计欣赏
  • 上海三凯建设管理咨询有限公司网站semaphore
  • 营销类网站建设公司网站制作
  • 做网站江门互联网销售平台
  • 武汉网站seo费用seo入门免费教程
  • 刘琪 找谁做网站靠谱ip切换工具
  • 汕头手机端建站模板今日国内新闻重大事件
  • 武汉建网站的网站揭阳百度快照优化排名
  • 洛阳恒凯做的网站有哪些优化关键词排名优化公司
  • h5响应式网站建设价格搜索引擎营销的特点是什么
  • wordpress手机接口合肥seo排名优化
  • 高唐做网站亚马逊关键词工具哪个最准
  • 天津网站建设-中国互联小程序推广引流
  • 用ps做网站的首页网址收录平台
  • 湛江公司做网站网站优化培训学校
  • 做淘客需要用的网站平台优化
  • 响应式网站建设制作需要注意什么深圳网站优化网站
  • 在线企业建站模板十大营销模式