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_front(40);
	L1.push_front(50);
	printList(L1);
	L1.pop_front();
	L1.insert(L1.begin(),100);
	list<int>::iterator it = L1.begin();
	L1.insert(++it, 100);
	printList(L1);

	it = L1.begin();
	L1.erase(++it);
	

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


}

int main()
{
	test01();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40214464/article/details/120824955