当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ forward_list::push_front()、forward_list::pop_front()用法及代码示例


STL中的转发列表实现单链接列表。从C++ 11引入的前向列表在插入,删除和移动操作(如排序)方面比其他容器有用,并且允许时间常数插入和删除元素。它与列表的不同之处在于前向列表会跟踪对象的位置仅list的下一个元素同时跟踪下一个和上一个元素。

forward_list::push_front

push_front()函数用于将元素从前面推入转发列表。在当前第一个元素和容器大小增加1之前,新值将从开头插入到“转发”列表中。

用法:


forwardlistname.push_front(value)
参数:
The value to be added in the front is 
passed as the parameter
Result:
Adds the value mentioned as the parameter to the
front of the forward list named as forwardlistname

例子:

Input:forward_list forwardlist{1, 2, 3, 4, 5};
        forwardlist.push_front(6);
Output:6, 1, 2, 3, 4, 5

Input:forward_list forwardlist{5, 4, 3, 2, 1};
        forwardlist.push_front(6);
Output:6, 5, 4, 3, 2, 1

错误和异常

1.强大的异常保证-如果引发异常,则容器中没有任何更改。
2.如果转发列表不支持作为参数传递的值,则它将显示未定义的行为。

// CPP program to illustrate 
// push_front() function 
#include <forward_list> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 }; 
    myforwardlist.push_front(6); 
  
    // Forward list becomes 6, 1, 2, 3, 4, 5 
  
    for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it) 
        cout << ' ' << *it; 
}

输出:

6 1 2 3 4 5

应用:使用push_front()函数输入具有以下编号和顺序的空的转发列表,并对给定的转发列表进行排序。

Input: 7, 89, 45, 6, 24, 58, 43
Output:6, 7, 24, 43, 45, 58, 89
// CPP program to illustrate 
// application Of push_front() function 
#include <forward_list> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    forward_list<int> myforwardlist{}; 
    myforwardlist.push_front(43); 
    myforwardlist.push_front(58); 
    myforwardlist.push_front(24); 
    myforwardlist.push_front(6); 
    myforwardlist.push_front(45); 
    myforwardlist.push_front(89); 
    myforwardlist.push_front(7); 
  
    // Forward list becomes 7, 89, 45, 6, 24, 58, 43 
    // Sorting function 
  
    myforwardlist.sort(); 
  
    for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it) 
        cout << ' ' << *it; 
}

输出量

6 7 24 43 45 58 89
forward_list::pop_front

pop_front()函数用于从正面弹出列表中删除元素。该值将从一开始就从列表中删除,并且容器大小减小1。

用法:

forwardlistname.pop_front()
参数:
No parameter is passed as the parameter.
Result:
Removes the value present at the front 
of the given forward list named as forwardlistname

例子:

Input:forward_list forwardlist{1, 2, 3, 4, 5};
        forwardlist.pop_front();
Output:2, 3, 4, 5

Input:forward_list forwardlist{5, 4, 3, 2, 1};
        forwardlist.pop_front();
Output:4, 3, 2, 1

错误和异常

1. No-Throw-Guarantee-如果引发异常,则容器中没有任何更改。
2.如果列表为空,则显示未定义的行为。

// CPP program to illustrate 
// pop_front() function 
#include <forward_list> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    forward_list<int> myforwardlist{ 1, 2, 3, 4, 5 }; 
    myforwardlist.pop_front(); 
  
    // forward list becomes 2, 3, 4, 5 
  
    for (auto it = myforwardlist.begin(); it != myforwardlist.end(); ++it) 
        cout << ' ' << *it; 
}

输出:

2 3 4 5

应用程序:使用push_front()函数输入具有以下编号和顺序的空前向列表,并打印列表的反面。

Input:1, 2, 3, 4, 5, 6, 7, 8
Output:8, 7, 6, 5, 4, 3, 2, 1
// CPP program to illustrate 
// application Of pop_front() function 
#include <forward_list> 
#include <iostream> 
using namespace std; 
  
int main() 
{ 
    forward_list<int> myforwardlist{}, newforwardlist{}; 
    myforwardlist.push_front(8); 
    myforwardlist.push_front(7); 
    myforwardlist.push_front(6); 
    myforwardlist.push_front(5); 
    myforwardlist.push_front(4); 
    myforwardlist.push_front(3); 
    myforwardlist.push_front(2); 
    myforwardlist.push_front(1); 
  
    // Forward list becomes 1, 2, 3, 4, 5, 6, 7, 8 
  
    while (!myforwardlist.empty()) { 
        newforwardlist.push_front(myforwardlist.front()); 
        myforwardlist.pop_front(); 
    } 
    for (auto it = newforwardlist.begin(); it != newforwardlist.end(); ++it) 
        cout << ' ' << *it; 
}

输出量

8 7 6 5 4 3 2 1


相关用法


注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 forward_list::push_front() and forward_list::pop_front() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。