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 数组最大元素