#include<iostream>
#include <cmath>
#include <forward_list>
using namespace std;
bool remove_this(const int& value){return (value<9);}
bool same_integral_part(double first,double second){
return (int(first)==int(second));
}
class is_near_class{
public:
bool operator()(double first,double second){
return (fabs(first-second)<5.0);
}
}is_near_object;
//forward_list是一种序列容器,允许对任何位置的常量时间的插入和删除操作.实际上是一种单向链表,forward_list是一种双向链表,一般list是双向链表;但是缺点是无法通过下标访问元素;
int main(){
//.assign常见的几种初始化方式
forward_list<int> first;
forward_list<int> second;
first.assign(4,15);
second.assign(first.begin(),first.end());
first.assign({7,12,33,4,5});
for(int& print:first)
cout << print << " ";
cout << endl;
//.before_begin():用来返回当前列表的第一个元素之前的位置,返回的位置是不能够被引用的;
first.insert_after(first.before_begin(),14);
for(int& print:first)
cout << print << " ";
cout << endl;
//.begin(),用于得到当前list第一个元素的位置
for(auto It=first.begin();It!=first.end();++It)
cout << *It << " ";
cout << endl;
//.cbefor_begin()用于返回一个const类型的迭代器,这个迭代器可以增减也可以减少,但是不能够更改指向;
first.insert_after(first.before_begin(),1);
for(auto It=first.begin();It!=first.end();++It)
cout << *It << " ";
cout << endl;
//.cbegin(),cend()当不进行list里面元素的修改时,可以使用cbegin()和cend()进行遍历;
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.clear()用于清空里面的元素;
second.clear();
for(auto It=second.cbegin();It!=second.cend();++It)
cout << *It << " ";
cout << endl;
//.emplace_after()以及.emplace_front()
auto It = first.begin();
It = first.emplace_after(It,11);
It = first.emplace_after(It,12);
It = first.emplace_after(It,14);
first.emplace_front(10);
first.emplace_front(9);
first.emplace_front(8);
first.emplace_front(7);
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.empty() 用于返回当前列表是否为空;
cout << boolalpha << first.empty() << noboolalpha << endl;
//.erase_after()用于删除某个元素之后的元素;
auto it = first.begin();
it = first.erase_after(it);
it = first.erase_after(it,first.end());
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.front()用于返回第一个元素的一个引用;
It = first.begin();
It = first.emplace_after(It,11);
It = first.emplace_after(It,12);
It = first.emplace_after(It,14);
first.emplace_front(10);
first.emplace_front(9);
first.emplace_front(8);
first.emplace_front(7);
first.front() = 99;
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.getallocator返回与关联容器分配对象的副本
//.insert_after() 可以在某个位置查入元素来改变容器的大小,在任何位置查入元素的时间代价都是一样的,再进行元素的插入
//时,不需要进行任何的复制移动操作;
It = first.begin();
It = first.insert_after(first.begin(),10);
It = first.insert_after(It,9);
It = first.insert_after(It,8);
It = first.insert_after(It,7);
It = first.insert_after(It,6);
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.merge()对于两个已经有序的链表进行合并,并且在合并之后仍然有序;
first.sort();
second.sort();
first.merge(second);
cout << "first contains: ";
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.=
second = first;
//.pop_front()用于删除最前面的元素;
second.pop_front();
//.push_front()用于在第一个位置添加元素;
first.push_front(10);
//.remove()用于删除链表里面的重复的元素;
first.remove(9);
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.remove_if() 需要通过一个函数来进行条件的判定;
first.remove_if(remove_this);
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.resize()用于更改大小;
second.resize(20);
for(auto It=second.cbegin();It!=second.cend();++It)
cout << *It << " ";
cout << endl;
//.reverse()用于翻转链表里面的元素
first.reverse();
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.sort()可以支持从小到大进行排序,也可以支持从大到小进行排序,默认是从小到大进行排序的;
first.sort(greater<int>());
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.splice_after()
It = first.begin();
first.splice_after(first.before_begin(),second);
second.splice_after(second.before_begin(),first,first.before_begin(),it);
first.splice_after(first.before_begin(),second,second.begin());
cout << "first: ";
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
for(auto It=second.cbegin();It!=second.cend();++It)
cout << *It << " ";
cout << endl;
//.用于交换两个list里面的内容;
first.swap(second);
cout << "first is: ";
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.unique():一共包含两个版本,第一个版本,void unique()表示的含义是从里面删除,连续重复元素除了第一个以外的元素,
//也就是处理之后,里面没有重复元素;
first.unique();
cout << "first.unique() is: ";
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
//.unique()的的第二个版本:template<class BinaryPredict> void unique(BinaryPredict binary_pred);表示的是接受一个
//对里面元素的处理规则,通常是函数形式;
first.unique(is_near_object);
cout << "first.unique(is_near_object)";
for(auto It=first.cbegin();It!=first.cend();++It)
cout << *It << " ";
cout << endl;
return 0;
}
forward_list
最新推荐文章于 2024-03-19 18:40:01 发布