STL中的转发列表实现单链列表。从C++ 11引入的前向列表在插入,删除和移动操作(如排序)方面比其他容器更有用,并且允许时间常数地插入和删除元素。
它与list的区别在于前向列表仅跟踪下一个元素的位置,而list跟踪下一个和上一个元素,因此增加了存储每个元素所需的存储空间。前向列表的缺点是不能向后迭代,并且不能直接访问其各个元素。
当仅需要向前遍历时(与单链表相比双链表更喜欢),前向列表比列表更可取,因为我们可以节省空间。一些示例情况是散列中的链,图的邻接表表示等。
转发列表上的操作:
1. Assign() :-此函数用于为转发列表分配值,其另一个变体用于分配重复的元素。
CPP
// C++ code to demonstrate forward list
// and assign()
#include
#include
using namespace std;
int main()
{
// Declaring forward list
forward_list flist1;
// Declaring another forward list
forward_list flist2;
// Assigning values using assign()
flist1.assign({1, 2, 3});
// Assigning repeating values using assign()
// 5 elements with value 10
flist2.assign(5, 10);
// Displaying forward lists
cout << "The elements of first forward list are : ";
for (int&a : flist1)
cout << a << " ";
cout << endl;
cout << "The elements of second forward list are : ";
for (int&b : flist2)
cout << b << " ";
cout << endl;
return 0;
}
CPP
// C++ code to demonstrate working of
// push_front(), emplace_front() and pop_front()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30, 40, 50};
// Inserting value using push_front()
// Inserts 60 at front
flist.push_front(60);
// Displaying the forward list
cout << "The forward list after push_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Inserting value using emplace_front()
// Inserts 70 at front
flist.emplace_front(70);
// Displaying the forward list
cout << "The forward list after emplace_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Deleting first value using pop_front()
// Pops 70
flist.pop_front();
// Displaying the forward list
cout << "The forward list after pop_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
CPP
// C++ code to demonstrate working of
// insert_after(), emplace_after() and erase_after()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30} ;
// Declaring a forward list iterator
forward_list::iterator ptr;
// Inserting value using insert_after()
// starts insertion from second position
ptr = flist.insert_after(flist.begin(), {1, 2, 3});
// Displaying the forward list
cout << "The forward list after insert_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Inserting value using emplace_after()
// inserts 2 after ptr
ptr = flist.emplace_after(ptr,2);
// Displaying the forward list
cout << "The forward list after emplace_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Deleting value using erase.after Deleted 2
// after ptr
ptr = flist.erase_after(ptr);
// Displaying the forward list
cout << "The forward list after erase_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
CPP
// C++ code to demonstrate working of remove() and
// remove_if()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30, 25, 40, 40};
// Removing element using remove()
// Removes all occurrences of 40
flist.remove(40);
// Displaying the forward list
cout << "The forward list after remove operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Removing according to condition. Removes
// elements greater than 20. Removes 25 and 30
flist.remove_if([](int x){ return x>20;});
// Displaying the forward list
cout << "The forward list after remove_if operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
CPP
// C++ code to demonstrate working of
// splice_after()
#include
#include // for splice_after()
using namespace std;
int main()
{
// Initializing forward list
forward_list flist1 = {10, 20, 30};
// Initializing second list
forward_list flist2 = {40, 50, 60};
// Shifting elements from first to second
// forward list after 1st position
flist2.splice_after(flist2.begin(),flist1);
// Displaying the forward list
cout << "The forward list after splice_after operation : ";
for (int&c : flist2)
cout << c << " ";
cout << endl;
return 0;
}
输出:
The elements of first forward list are : 1 2 3
The elements of second forward list are : 10 10 10 10 10
2. push_front() :-此函数用于在向前列表的第一个位置插入元素。该函数的值复制到容器中第一个元素之前的空间。转发列表的大小增加1。
3. emplace_front() :-此函数与先前的函数相似,但是由于没有复制操作,因此将在向前列表的第一个元素之前的内存中直接创建该元素。
4. pop_front() :-此函数用于删除列表的第一个元素。
CPP
// C++ code to demonstrate working of
// push_front(), emplace_front() and pop_front()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30, 40, 50};
// Inserting value using push_front()
// Inserts 60 at front
flist.push_front(60);
// Displaying the forward list
cout << "The forward list after push_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Inserting value using emplace_front()
// Inserts 70 at front
flist.emplace_front(70);
// Displaying the forward list
cout << "The forward list after emplace_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Deleting first value using pop_front()
// Pops 70
flist.pop_front();
// Displaying the forward list
cout << "The forward list after pop_front operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
输出:
The forward list after push_front operation : 60 10 20 30 40 50
The forward list after emplace_front operation : 70 60 10 20 30 40 50
The forward list after pop_front operation : 60 10 20 30 40 50
4. insert_after()此函数使我们可以选择在向前列表中的任何位置插入元素。此函数中的参数将复制到所需位置。
5. emplace_after()此函数还执行与上述函数相同的操作,但无需任何复制操作即可直接制作元素。
6. delete_after() 此函数用于从转发列表中的特定位置删除元素。
CPP
// C++ code to demonstrate working of
// insert_after(), emplace_after() and erase_after()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30} ;
// Declaring a forward list iterator
forward_list::iterator ptr;
// Inserting value using insert_after()
// starts insertion from second position
ptr = flist.insert_after(flist.begin(), {1, 2, 3});
// Displaying the forward list
cout << "The forward list after insert_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Inserting value using emplace_after()
// inserts 2 after ptr
ptr = flist.emplace_after(ptr,2);
// Displaying the forward list
cout << "The forward list after emplace_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Deleting value using erase.after Deleted 2
// after ptr
ptr = flist.erase_after(ptr);
// Displaying the forward list
cout << "The forward list after erase_after operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
输出:
The forward list after insert_after operation : 10 1 2 3 20 30
The forward list after emplace_after operation : 10 1 2 3 2 20 30
The forward list after erase_after operation : 10 1 2 3 2 30
7. remove() :-此函数从其参数中提到的转发列表中删除特定元素。
8. remove_if() :-此函数根据其参数中的条件将其删除。
CPP
// C++ code to demonstrate working of remove() and
// remove_if()
#include
#include
using namespace std;
int main()
{
// Initializing forward list
forward_list flist = {10, 20, 30, 25, 40, 40};
// Removing element using remove()
// Removes all occurrences of 40
flist.remove(40);
// Displaying the forward list
cout << "The forward list after remove operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
// Removing according to condition. Removes
// elements greater than 20. Removes 25 and 30
flist.remove_if([](int x){ return x>20;});
// Displaying the forward list
cout << "The forward list after remove_if operation : ";
for (int&c : flist)
cout << c << " ";
cout << endl;
return 0;
}
输出:
The forward list after remove operation : 10 20 30 25
The forward list after remove_if operation : 10 20
9. splice_after() :-此函数将元素从一个前向列表传输到另一个。
CPP
// C++ code to demonstrate working of
// splice_after()
#include
#include // for splice_after()
using namespace std;
int main()
{
// Initializing forward list
forward_list flist1 = {10, 20, 30};
// Initializing second list
forward_list flist2 = {40, 50, 60};
// Shifting elements from first to second
// forward list after 1st position
flist2.splice_after(flist2.begin(),flist1);
// Displaying the forward list
cout << "The forward list after splice_after operation : ";
for (int&c : flist2)
cout << c << " ";
cout << endl;
return 0;
}
输出:
The forward list after splice_after operation : 40 10 20 30 50 60
forward_list的其他一些方法:
- front()–此函数用于引用转发列表容器的第一个元素。
- begin()– begin()函数用于返回指向前向列表容器的第一个元素的迭代器。
- end()– end()函数用于返回指向列表容器最后一个元素的迭代器。
- cbegin()–返回一个常量迭代器,该常量指向forward_list的第一个元素。
- cend()–返回一个常量迭代器,该常量指向forward_list的last-the-last元素。
- before_begin()–返回一个迭代器,该迭代器指向forward_list的第一个元素之前的位置。
- cbefore_begin()–返回一个常量的随机访问迭代器,该迭代器指向forward_list的第一个元素之前的位置。
- max_size()–返回forward_list可以容纳的最大元素数。
- resize()–更改forward_list的大小。