c++stl和std_C ++ STL转发列表容器– std :: forward_list

c++stl和std

In this tutorial you will learn about C++ STL forward list i.e., std::forward_list and operations, methods applicable on it.

在本教程中,您将了解C ++ STL转发列表,即std :: forward_list和操作以及适用于它的方法。

Forward Lists come under sequence containers. Forward List implements singly linked list. Insertion, removal and moving operations are very fast than other containers. Every element of forward list contains its next element address. Main disadvantage of forward list is that cannot be iterated backwards and its individual elements cannot be accessed directly. When singly linked list preferred over double linked list, Forward list is better than List. Because list behaves a like double linked list. Example for where we can use forward list is chaining in hashing, adjacency list representation of graph etc.

转发列表位于序列容器下。 转发列表实现单链表。 插入,移除和移动操作比其他容器要快得多。 转发列表的每个元素都包含其下一个元素地址。 前向列表的主要缺点是不能向后迭代,并且不能直接访问其各个元素。 当单链表比双链表优先时,转发表比列表更好。 因为列表的行为类似于双重链接列表。 我们可以使用转发列表的示例是散列中的链接,图的邻接列表表示等。

Also Read: C++ STL List Container – std::list

另请阅读: C ++ STL列表容器– std :: list

C ++ STL转发列表 (C++ STL Forward List)

Now let’s see what are the operations we can apply on forward list:

现在,让我们看看可以对转发列表执行哪些操作:

assign(): Just like insert method, assign() will store the values. By using assign() we can insert elements in two ways.

Assign():就像insert方法一样,assign()将存储值。 通过使用assign(),我们可以通过两种方式插入元素。

First way is we can insert what are the elements we required. Second way is we can insert a single element “n” number of times. But in second way of assignment list will be replaced by new values. Old list values will be erased.

第一种方法是我们可以插入所需的元素。 第二种方法是我们可以插入单个元素“ n”次。 但是以第二种方式,分配列表将被新值替换。 旧列表值将被删除。

Try this below code for more understanding:

尝试下面的代码以获得更多理解:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list<int> lst1; // declaration of a list
    forward_list<int> :: iterator it; // iterator for list
 
    // assigning values into list.
    lst1.assign({10,20,30});
 
    for(it = lst1.begin(); it!= lst1.end(); ++it)
        cout << *it << " ";
    
    cout << endl;
 
    // using assign() method in other way
    lst1.assign(3,99);
    // assigning three elements of each value 99
    
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

输出量

10 20 30 99 99 99

10 20 30 99 99 99

插入功能 (Insert Functions)

push_front(): This function used to insert new value at beginning of the list. The value from this function copied to the address before to the current first element of container.

push_front():此函数用于在列表的开头插入新值。 该函数的值复制到容器的当前第一个元素之前的地址。

emplace_front(): This function also used to insert elements at beginning of the container but there no copy operation. Here element directly inserts at the address before to the first element.

emplace_front():此函数还用于在容器的开头插入元素,但没有复制操作。 在这里,元素直接插入第一个元素之前的地址。

insert_after(): Using this function we can insert element at any position of the forward list. The arguments which we pass to this function will be copied to particular location.

insert_after():使用此函数,我们可以在转发列表的任何位置插入元素。 我们传递给该函数的参数将被复制到特定位置。

emplace_after(): This also works same as insert_after() function. But element directly insert without any copy operation.

emplace_after():这也与insert_after()函数相同。 但是元素可以直接插入而无需任何复制操作。

Example program to show above insert functions:

显示上述插入功能的示例程序:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> :: iterator it;
    forward_list <int> :: iterator it1;
    
    lst1.assign({10,20,30}); // initially these 3 values are in forward list
 
    // using push front method
    lst1.push_front(5);
 
    // using emplace_front to insert at front
    lst1.emplace_front(4);
 
    // check the result
    for(it= lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    // using insert_after function
    it1 = lst1.insert_after(lst1.begin(),99); // inserting 99 after current first element
 
    // using emplace_after function
    lst1.emplace_after(it1,0); // inserting 0 after which it1 iterator pointing to.
 
    // checking the result
    for(it = lst1.begin(); it!= lst1.end(); it++)
    	cout << *it << " ";
    
    cout << endl;
 
    return 0;
}

Output

输出量

4 5 10 20 30 4 99 0 5 10 20 30

4 5 10 20 30 4 99 0 5 10 20 30

删除功能 (Delete Functions)

pop_front(): This function removes the first element of the forward list.

pop_front():此函数删除转发列表的第一个元素。

erase_after(): This function used to delete the element at particular position of forward list.

delete_after():此函数用于删除转发列表特定位置的元素。

remove(): This function removes the specific element which we pass as an argument to this function

remove():此函数删除我们作为该函数的参数传递的特定元素

remove_if(): This function removes the element we specified only the condition which we gives is true.

remove_if():此函数删除仅我们指定的条件为true的元素。

Example program to show above erasing functions of forward list:

示例程序显示上述擦除列表的功能:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> :: iterator it;
 
    lst1.assign({10,20,30,40,50});
    // initially we assigned some values into forward list
 
    // deleting first element
    lst1.pop_front();
 
    // deleting element at particular position using erase after function
    it = lst1.begin();
    lst1.erase_after(it);
 
    // check the result
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    // deleting an element using remove function
    lst1.remove(50); // removing element 50 in forward list
 
    // deleting element by condition
    lst1.remove_if([](int a){ return a>25;});
 
    // check the result
    for(it = lst1.begin(); it!= lst1.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

输出量

20 40 50 20

20 40 50 20

Some more functions are:

其他一些功能是:

splice_after(): This function used to transfer one forward list elements to other forward list. It places the elements after specified position only.

splice_after():此函数用于将一个转发列表元素转移到其他转发列表。 它将元素仅放置在指定位置之后。

reverse(): This function reverse all elements of the forward list.

reverse():此函数反向转发列表的所有元素。

sort(): This function sorts the elements in the forward list.

sort():此函数对转发列表中的元素进行排序。

Example program to explain above functions:

解释上述功能的示例程序:

#include<iostream>
#include<forward_list>
 
using namespace std;
 
int main(){
    forward_list <int> lst1;
    forward_list <int> lst2;
    forward_list <int> :: iterator it;
    
    lst1.assign({20,40,60});
    lst2.assign({30,50,70});
 
    // now adding forward list1 elements to forward list2 using splice_after function after first element in this list
    lst2.splice_after(lst2.begin(),lst1);
 
    // Checking after adding forward list1 to forward list2
    cout << "after adding forward list1 into forward list2" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
 
    lst2.reverse(); // preforming reverse operation on forward list2
 
    // printing revere elements
    cout << "after reversing forward list2" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
   
    cout << endl;
 
    // sorting elements
    lst2.sort();
    cout << "after sorting the elements" << endl;
    for (it= lst2.begin(); it!= lst2.end(); ++it)
    	cout << *it << " ";
    
    cout << endl;
    
    return 0;
}

Output

输出量

after adding forward list1 into forward list2 30 20 40 60 50 70 after reversing forward list2 70 50 60 40 20 30 after sorting the elements 20 30 40 50 60 70

在将 元素 20 30 40 50 60 70 排序 后,将正向列表1添加到正向列表2之后 30 30 40 40 60 50 70将 反向列表2 70 50 60 40 20 30 反向

Comment below if you have queries or found any information incorrect in above tutorial for C++ STL forward list.

如果您有疑问或在上面的C ++ STL转发列表教程中发现任何不正确的信息,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2017/08/stl-forward-list.html

c++stl和std

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值