C++ list容器 大小操作

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

void printList(const list<int>& L)
{
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;
}
void test01()
{
	list<int>L1; //默认构造
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	L1.push_back(50);
	printList(L1);

	//区间方式构造
	if (L1.empty()) {
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的元素个数为:"<<L1.size() << endl;
	}

	//重新指定其大小
	L1.resize(10);
	printList(L1);
	L1.resize(2);
	printList(L1);
	L1.resize(10);
	printList(L1);
}

int main()
{
	test01();
	system("pause");
	return 0;
}
全部评论

相关推荐

活泼的代码渣渣在泡池...:哈哈哈挺好的,我也上岸美团了,不说了,我又接了一单
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务