c ++ stl
Given a list with some of the elements, we have to insert an element at the front (beginning) and an element at the back (end) to the list in C++ (STL) program.
给定一个包含一些元素的列表,我们必须在C ++(STL)程序的列表的开头(开头)和后面(结尾)插入一个元素。
列表的Push_front()和push_back()函数 (Push_front() and push_back() functions of the list)
These are two functions which can be used to insert the element at the front and at the end to the list. push_front() inserts the element at the front and push_back() inserts the element at the back (end).
这是两个函数,可用于在列表的开头和结尾插入元素。 push_front()将元素插入到前面,而push_back()将元素插入到后面(结束)。
Let's implement the program below...
让我们实现下面的程序...
Example:
例:
Input:
List: [10, 20, 30, 40, 50]
Element to insert at front: 100
Element to insert at back: 200
Output:
List is:
100
10
20
30
40
50
200
Program:
程序:
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main()
{
//declaring aiList
list<int>iList = {10, 20, 30, 40, 50};
//declaring iterator to the list
list<int>::iterator l_iter;
//inserting element at the front
iList.push_front(100);
//inserting element at the back
iList.push_back(200);
//printing list elements
cout<<"List elements are"<<endl;
for (l_iter = iList.begin(); l_iter != iList.end(); l_iter++)
cout<< *l_iter<<endl;
return 0;
}
Output
输出量
List elements are
100
10
20
30
40
50
200
翻译自: https://www.includehelp.com/stl/insert-the-element-at-beginning-and-end-of-the-list.aspx
c ++ stl