list::push_front()是C++ STL中的內置函數,用於在當前頂部元素之前的列表容器的前麵插入元素。此函數還將容器的大小增加1。
用法:
list_name.push_front(dataType value)
參數:此函數接受單個參數值。此參數表示需要在列表容器的前麵插入的元素。
返回值:此函數不返回任何內容。
以下示例程序旨在說明C++ STL中的list::push_front()函數:
// CPP program to illustrate the 
// list::push_front() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // Creating a list 
    list<int> demoList; 
  
    // Adding elements to the list 
    // using push_back() 
    demoList.push_back(10); 
    demoList.push_back(20); 
    demoList.push_back(30); 
    demoList.push_back(40); 
  
    // Initial List: 
    cout << "Initial List: "; 
    for (auto itr = demoList.begin(); itr != demoList.end(); itr++) 
        cout << *itr << " "; 
  
    // Adding elements to the front of List 
    // using push_front 
    demoList.push_front(5); 
  
    // List after adding elements to front 
    cout << "\n\nList after adding elements to the front:\n"; 
    for (auto itr = demoList.begin(); itr != demoList.end(); itr++) 
        cout << *itr << " "; 
  
    return 0; 
}
輸出:
Initial List: 10 20 30 40 List after adding elements to the front: 5 10 20 30 40
相關用法
- C++ list end()用法及代碼示例
- C++ list max_size()用法及代碼示例
- C++ list remove()用法及代碼示例
- C++ list splice()用法及代碼示例
- C++ list emplace()用法及代碼示例
- C++ list assign()用法及代碼示例
- C++ list push_back()用法及代碼示例
- C++ list back()用法及代碼示例
- C++ list front()用法及代碼示例
- C++ list pop_front()用法及代碼示例
- C++ list pop_back()用法及代碼示例
- C++ list empty()用法及代碼示例
- C++ list reverse用法及代碼示例
- C++ list merge()用法及代碼示例
- C++ list size()用法及代碼示例
注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 list push_front() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
