c ++ stl_在列表的开头和结尾处插入元素| C ++ STL

本文介绍了如何在C++ STL程序中使用list的push_front()和push_back()函数来在列表的开头和末尾插入元素。push_front()用于在列表前端插入,而push_back()用于在列表尾部插入。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值