#include <iostream>
#include <string>
#include <forward_list> using namespace std; // https://zh.cppreference.com/w/cpp/container/forward_list std::ostream& operator<<(std::ostream& ostr, const std::forward_list<int>& list)
{
for (auto &i : list) {
ostr << " " << i;
}
return ostr;
} class item
{
public:
item() = delete; item(const int& a, const int& b)
: m_a(a), m_b(b)
{} bool operator< (const item& comp) const
{
return m_a*m_b < comp.m_a*comp.m_b;
} bool operator== (const item& comp) const
{
return m_a==comp.m_a && m_b==comp.m_b;
} private:
int m_a;
int m_b;
}; bool compAB(int a, int b)
{
return a + 1 < b;
} int main()
{
int a[] = { 1,2,3 };
forward_list<int> lis;
forward_list<int> lis2(4);
forward_list<int> lis3(4, 5);
forward_list<item> lis5(4, item(1, 2));
forward_list<item> lis6(4, { 1, 2 });
forward_list<item> lis7(lis6);
forward_list<int> lis8(a, a + 3);
forward_list<item> lis9(lis7.begin(), lis7.end());
forward_list<int> lis4({ 1,2,3 }); ////////////////////////////////////////////////////////////////////////// int v_front = lis4.front(); std::forward_list<int>::iterator
it_bb = lis4.before_begin(); // 返回指向第一个元素之前迭代器
auto cit_bb = lis4.cbefore_begin(); auto it_b = lis4.begin();
auto cit_b = lis4.cbegin(); auto it_e = lis4.end();
auto cit_e = lis4.cend(); ////////////////////////////////////////////////////////////////////////// // 没有 size bool isEmpty = lis4.empty();
size_t max_size = lis4.max_size(); // 并非实际可以申请到的大小 ////////////////////////////////////////////////////////////////////////// lis4.clear();
lis4 = { 1,2,3 }; // iterator insert_after(const_iterator pos, const T& value);
lis4.insert_after(lis4.before_begin(), 77); // 78 1 2 3
lis4.insert_after(lis4.begin(), 78); // 77 78 1 2 3 // iterator insert_after( const_iterator pos, size_type count, const T& value );
lis4.insert_after(lis4.before_begin(), 2, 79); // 79 79 78 1 2 3 forward_list<int> lis10({ 10,20,30 });
// template< class InputIt >
// iterator insert_after(const_iterator pos, InputIt first, InputIt last);
lis4.insert_after(lis4.before_begin(), lis10.begin(), lis10.end()); // 10 20 30 79 79 78 1 2 3 lis4.insert_after(lis4.before_begin(), { 11,22 }); // 11 22 10 20 30 79 79 78 1 2 3 ////////////////////////////////////////////////////////////////////////// // 在容器中的指定位置后插入新元素。原位构造元素,即不进行复制或移动操作。
lis4.emplace_after(lis4.begin(), 555); // 11 555 22 10 20 30 79 79 78 1 2 3 // iterator erase_after( const_iterator pos );
// 从容器移除指定元素。
lis4.erase_after(lis4.begin()); // 11 22 10 20 30 79 79 78 1 2 3 ////////////////////////////////////////////////////////////////////////// lis4.push_front(666); // 666 11 22 10 20 30 79 79 78 1 2 3
lis4.emplace_front(777); // 777 666 11 22 10 20 30 79 79 78 1 2 3
lis4.pop_front(); // 666 11 22 10 20 30 79 79 78 1 2 3 ////////////////////////////////////////////////////////////////////////// lis4.resize(2); // 缩小,保留前两个
lis4.resize(5); // 放大,补默认值,这里是 0 lis4.swap(lis8); ////////////////////////////////////////////////////////////////////////// // merge: 排序后,把 2 的元素 移动 到 1 中。
{
std::forward_list<int> list1 = { 5,9,0,1,3,10,0 };
std::forward_list<int> list2 = { 8,7,2,6,4 }; list1.sort();
list2.sort();
std::cout << "list1: " << list1 << "\n";
std::cout << "list2: " << list2 << "\n";
list1.merge(list2); // 必须是有序的,元素的 < 必须有定义。// lis2 变为空
std::cout << "merged: " << list1 << "\n";
} {
std::forward_list<int> list1 = { 5,9,0,1,3 };
std::forward_list<int> list2 = { 8,7,2,6,4 }; list1.sort();
list2.sort();
std::cout << "list1: " << list1 << "\n";
std::cout << "list2: " << list2 << "\n";
list1.merge(list2, [](int a, int b) { return a + 1 < b; }); // 自定义 < 的比较方法,lambda 表达式。// lis2 变为空
std::cout << "merged: " << list1 << "\n";
} {
std::forward_list<int> list1 = { 5,9,0,1,3 };
std::forward_list<int> list2 = { 8,7,2,6,4 }; list1.sort();
list2.sort();
std::cout << "list1: " << list1 << "\n";
std::cout << "list2: " << list2 << "\n";
list1.merge(list2, compAB); // 函数名。// lis2 变为空
std::cout << "merged: " << list1 << "\n";
} ////////////////////////////////////////////////////////////////////////// {
forward_list<int> lis11({ 10,20,30 });
forward_list<int> lis12({ 101,201,301 }); // void splice_after( const_iterator pos, forward_list& other );
lis11.splice_after(lis11.before_begin(), lis12); // 101,201,301 10,20,30
} {
forward_list<int> lis11({ 10,20,30 });
forward_list<int> lis12({ 101,201,301 });
// void splice_after( const_iterator pos, forward_list& other, const_iterator it );
lis11.splice_after(lis11.before_begin(), lis12, lis12.begin()); // 201 10,20,30 // 迭代器的后一个元素
} {
forward_list<int> lis11({ 10,20,30 });
forward_list<int> lis12({ 101,201,301 });
auto it12 = lis12.begin();
std::advance(it12, 2);
// void splice_after( const_iterator pos, forward_list& other,
// const_iterator first, const_iterator last );
lis11.splice_after(lis11.before_begin(), lis12, lis12.before_begin(), it12); // 101,201 10,20,30 // 最后的两个迭代器,是开集
} ////////////////////////////////////////////////////////////////////////// {
std::forward_list<int> l = { 1,100,2,3,10,1,11,-1,12 }; l.remove(1); // 移除两个等于 1 的元素
l.remove_if([](int n) { return n > 10; }); // 移除全部大于 10 的元素 for (int n : l) {
std::cout << n << ' ';
}
std::cout << '\n';
} ////////////////////////////////////////////////////////////////////////// // 逆转容器中的元素顺序。不非法化任何引用或迭代器。
forward_list<int> lis13({ 10,20,30 });
lis13.reverse(); //////////////////////////////////////////////////////////////////////////
// 从容器移除所有 相邻 的重复元素。只留下相等元素组中的第一个元素。
{
std::forward_list<int> x = { 1, 2, 2, 3, 3, 2, 1, 1, 2 }; std::cout << "contents before:";
for (auto val : x)
std::cout << ' ' << val;
std::cout << '\n'; x.unique(); // 1 2 3 2 1 2
std::cout << "contents after unique():";
for (auto val : x)
std::cout << ' ' << val;
std::cout << '\n'; return 0;
} //////////////////////////////////////////////////////////////////////////
// sort
{
std::forward_list<int> list = { 8,7,5,9,0,1,3,2,6,4 }; std::cout << "before: " << list << "\n";
list.sort(); // 默认升序
std::cout << "ascending: " << list << "\n";
list.sort(std::greater<int>()); // 降序
std::cout << "descending: " << list << "\n"; // 类,要重载 <
// 或者,传参 lambda 表达式
} }

  

C++ std::forward_list 基本用法的更多相关文章

  1. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  2. C++ std::list 基本用法

    #include <iostream> #include <string> #include <list> using namespace std; // http ...

  3. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  4. QLinkedList和std::forward_list

    forward_list forward_list是C++11版本才有的.forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些.forwa ...

  5. QLinkedList和std::forward_list(都是双向链表,不支持operator[],好处可能是插入和删除都比较快)

    forward_list forward_list是C++11版本才有的.forward_list被实现为单链表,而list是一个双向链表,所以forward_list要比list高效一些.forwa ...

  6. C++ std::pair的用法

    1 pair的应用 pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用是,当一个函数需要返回2个数据的时候, ...

  7. std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题

    在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...

  8. C++ std::list 和 std::forward_list 的差别及其成员函数差异对比

    主要差别: list 是双向链表,forward_list 是双向链表. 成员函数差异: 函数名 list forward_list back() has no size() has no inser ...

  9. 容器之分类与各种测试(三)——forward_list的用法

    forward_list是C++11规定的新标准单项链表,slist是g++以前的规定的单项链表 例程 #include<stdexcept> #include<string> ...

随机推荐

  1. Everspin非易失性MRAM切换技术

    切换MRAM技术 切换MRAM使用1个晶体管,1个MTJ单元来提供简单的高密度存储器.Everspin使用获得专利的Toggle电池设计,可提供高可靠性.数据在温度下20年始终是非易失性的. 在读取期 ...

  2. Day 03 作业

    简述变量的组成 变量名,赋值符号,变量值 简述变量名的命名规范 变量名应该能反映变量值所描述的状态 变量名必须以字母数字下划线组合且不能以数字开头 变量名不能是关键字 简述注释的作用 让后面的代码失效 ...

  3. 成为java高手的成长历程想学好java必看

    1:J2SE入门阶段(3-6个月) 学习内容:J2SE常用类,JDBC等等 动态绑定,反射机制等 2:J2EE入门阶段(3个月) 学习内容:JSP/Servlet/JavaBean MVC结构的概念 ...

  4. Appium移动端自动化测试--控件定位方法

    常用定位手段 id Accessibility ID XPath 控件基础知识 DOM: Document Object Model文档对象模型 DOM应用:最早应用于HTML和Javascript的 ...

  5. webpack前期了解

    webpack的核心概念(四个) 入口(entry) 输出(output) loader 插件(plugins) Entry(入口)——指示 webpack 应该使用哪个模块,来作为构建其内部依赖图的 ...

  6. IIS配置svc(IIS8中添加WCF支持几种方法小结)

    方法一 最近在做Silverlight,Windows Phone应用移植到Windows 8平台,在IIS8中测试一些传统WCF服务应用,发现IIS8不支持WCF服务svc请求,后来发现IIS8缺少 ...

  7. vue中点击屏幕其他区域关闭自定义div弹出框

    直接上代码: mounted: function () { let that = this; $(document).on('click', function (e) { let dom = $('. ...

  8. C#程序编写高质量代码改善的157个建议【4-9】[TryParse比Parse、使用int?来确保值类型也可以为null、readonly和const、0值设为枚举的默认值、避免给枚举类型的元素提供显式的值、习惯重载运算符]

    建议4.TryParse比Parse好 如果注意观察,除string之外的所有的基元类型.会发现它们都有两个将字符串转换为自身类型的方法:Parse和TryParse.以类型double为例. 两者最 ...

  9. MongoDB(八):索引

    1. 索引 索引支持查询的有效地提高效率.没有索引,MongoDB必须扫描集合的每个文档,以选择与查询语句匹配的文档.这种扫描效率很低,需要MongoDB处理大量的数据. 索引是特殊的数据结构,以易于 ...

  10. CVE-2018-12613-phpmyadmin4.8.1远程文件包含漏洞复现

    CVE-2018-12613-phpmyadmin4.8.1远程文件包含漏洞复现 参考文章1 参考文章2 By:Mirror王宇阳 漏洞原理 攻击者利用发现在服务器上包含(查看和潜在执行)文件的漏洞. ...