C++ 1中的前向表(介绍和重要函数)

STL中的转发列表实现了单链表。forward list是从C++11引入的,在插入、删除和移动操作(如排序)中,它比其他容器更有用,并且允许以时间常量插入和删除元素。

null

它不同于 列表 由于转发列表只跟踪下一个元素的位置,而列表同时跟踪下一个和上一个元素,因此增加了存储每个元素所需的存储空间。前向列表的缺点是不能向后迭代,并且不能直接访问其单个元素。

当只需要向前遍历时(与单链表优于双链表相同),向前列表优于列表,因为我们可以节省空间。例如,哈希中的链接、图的邻接列表表示等。

转发列表上的操作: 1. 分配() : 此函数用于为转发列表分配值,其另一个变量用于分配重复的元素。

CPP

// C++ code to demonstrate forward list
// and assign()
#include <forward_list>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Declaring forward list
forward_list< int > flist1;
// Declaring another forward list
forward_list< int > 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;
}


输出

The elements of first forward list are : 1 2 3 The elements of second forward list are : 10 10 10 10 10 

2. 向前推 : 此函数用于在转发列表的第一个位置插入元素。此函数的值将复制到容器中第一个元素之前的空间。转发列表的大小增加1。

3. 安置(前方) : 此函数与上一个函数类似,但在这种情况下,不会发生复制操作,而是在转发列表的第一个元素之前直接在内存中创建元素。

4. pop_front() : 此函数用于删除列表的第一个元素。

CPP

// C++ code to demonstrate working of
// push_front(), emplace_front() and pop_front()
#include <forward_list>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Initializing forward list
forward_list< int > 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 

5. 在()后面插入_ : 这个函数让我们可以选择在转发列表的任何位置插入元素。将此函数中的参数复制到所需位置。

6. 在()之后放置 此功能也执行与上述功能相同的操作,但元件直接制作,无需任何复制操作。

7. 删除()之后的内容: 此功能用于删除转发列表中特定位置的元素。

CPP

// C++ code to demonstrate working of
// insert_after(), emplace_after()
// and erase_after()
#include <forward_list>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Initializing forward list
forward_list< int > flist = { 10, 20, 30 };
// Declaring a forward list iterator
forward_list< int >::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 

8. 删除() : 此函数用于从其参数中提到的转发列表中删除特定元素。

9 删除_if() : 此函数根据其参数中的条件移除。

CPP

// C++ code to demonstrate
// working of remove() and
// remove_if()
#include <forward_list>
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Initializing forward list
forward_list< int > 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 

10 在…之后 : 此函数用于将元素从一个转发列表转移到另一个转发列表。

CPP

// C++ code to demonstrate working of
// splice_after()
#include <forward_list> // for splice_after()
#include <iostream>
using namespace std;
// Driver Code
int main()
{
// Initializing forward list
forward_list< int > flist1 = { 10, 20, 30 };
// Initializing second list
forward_list< int > 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 

转发列表的其他一些方法:

方法

释义

前() 此函数用于引用转发列表容器的第一个元素。
开始 此函数用于返回指向转发列表容器的第一个元素的迭代器。
结束() 此函数用于返回指向列表容器最后一个元素的迭代器。
cbegin() 返回指向forward_列表的第一个元素的常量迭代器。
cend() 返回一个常量迭代器,该迭代器指向forward_列表最后一个元素的过去。
在开始之前 返回一个迭代器,该迭代器指向forward_列表第一个元素之前的位置。
在开始之前 返回一个常量随机访问迭代器,该迭代器指向forward_列表第一个元素之前的位置。
最大尺寸() 返回forward_list可容纳的最大元素数。
调整大小() 更改转发列表的大小。
独特的 从转发列表中删除所有连续的重复元素。它使用二进制谓词进行比较。
反向 反转正向列表中元素的顺序。

第二组参考本文—— C++ 2集合中的前向列表(操作函数)

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享