stl 数组最大元素_分配带有数组元素的列表| C ++ STL

stl 数组最大元素

Given an array and we have to create a list, that should be assigned with all elements of the array using C++ (STL) program.

给定一个数组,我们必须创建一个列表,该列表应使用C ++(STL)程序分配给该数组的所有元素。

Example:

例:

Here, we are declaring a list list1 and an array arr, arr has 5 elements, all elements of the arr are assigning to the list list1 by using following statements,

在这里,我们声明一个列表list1和一个数组arr , arr有5个元素,使用以下语句将arr的所有元素分配给列表list1 ,

    list1.assign(arr+0, arr+5);

Here,

这里,

  • list1 is a list of integer type

    list1是整数类型的列表

  • arr is an integer array

    arr是一个整数数组

  • arr+0 points the first element of the array

    arr + 0指向数组的第一个元素

  • And, arr+4 points the last element of the array

    而且, arr + 4指向数组的最后一个元素

Program:

程序:

#include <iostream>
#include <list>
using namespace std;

//function to display the list
void dispList(list<int> L)
{
	//declaring iterator to the list
	list<int>::iterator l_iter;
	for (l_iter = L.begin(); l_iter != L.end(); l_iter++)
		cout<< *l_iter<< " ";
	cout<<endl;
}

int main()
{
	//list1 declaration
	list<int> list1;
	//array declaration
	int arr[]={10, 20, 30, 40, 50};

	//displaying list1
	cout<<"Before assign... "<<endl;
	cout<<"Size of list1: "<<list1.size()<<endl;
	cout<<"Elements of list1: ";
	dispList(list1);

	//assigning array Elements to the list
	list1.assign(arr+0, arr+5);

	//displaying list1
	cout<<"After assigning... "<<endl;
	cout<<"Size of list1: "<<list1.size()<<endl;
	cout<<"Elements of list1: ";
	dispList(list1);

	return 0;
}

Output

输出量

Before assign...
Size of list1: 0
Elements of list1:
After assigning...
Size of list1: 5
Elements of list1: 10 20 30 40 50 


翻译自: https://www.includehelp.com/stl/assign-a-list-with-array-elements.aspx

stl 数组最大元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值