18.7.2 list构造函数

  • Post author:
  • Post category:其他




18.7.2 list构造函数

函数原型:


  • list<T> lst;

    //默认构造函数

  • list(const list &lst);

    //拷贝构造函数

  • list(begin, end);

    //将[begin, end)区间元素拷贝给本身

  • list(n, ele);

    //将n个ele元素拷贝给本身


示例:

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

template<class T>
void printList(const list<T>& L)
{
	for (list<T>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << '\t';
	}
	cout << endl;
}

void test1()
{
	//默认构造
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	//区间方式构造
	list<int>L2(L1.begin(), L1.end());
	printList(L2);

	//拷贝构造
	list<int>L3(L2);
	printList(L3);

	//n个elem
	list<int>L4(10, 1000);
	printList(L4);
}

int main()
{
	test1();
}

在这里插入图片描述



版权声明:本文为qq_32513033原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。