當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。