将元素分配给列表(不同的方法)-list :: assign()的示例| C ++ STL

C ++ STL中的list :: assign() (list::assign() in C++ STL)

It assigns or replaces the elements to the list.

它将元素分配或替换到列表中。

Example:

例:

In the given example, there are 3 different methods are used to assign/replace elements to the list.

在给定的示例中,有3种不同的方法用于为列表分配/替换元素。

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

//function to display the list
void dispList(list<int> L)
{
	//declaring interator 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()
{
	list<int> list1;
	list<int> list2;
	list<int> list3;
	list<int> list4;

	//assigning 100 to 5 elements
	list1.assign(5,100);
	cout<<"size of list1: "<<list1.size()<<endl;
	cout<<"Elements of list1: ";
	dispList(list1);

	//assigning all elements of list1 to list2
	list2.assign(list1.begin(), list1.end());
	cout<<"size of list2: "<<list2.size()<<endl;
	cout<<"Elements of list2: ";
	dispList(list2);

	//assigning list by array elements
	int arr []={10, 20, 30, 40};
	list3.assign(arr, arr+4);
	cout<<"size of list3: "<<list3.size()<<endl;
	cout<<"Elements of list3: ";
	dispList(list3);
	return 0;
}

Output

输出量

size of list1: 5
Elements of list1: 100 100 100 100 100
size of list2: 5
Elements of list2: 100 100 100 100 100
size of list3: 4
Elements of list3: 10 20 30 40  

Ref: list::assign()

参考: list :: assign()

翻译自: https://www.includehelp.com/stl/assign-the-elements-to-the-list-different-methods.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值