c++的list容器的基本操作

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
using namespace std;
void test01()
{
	list::iterator it;
	it++;
	it--;
	//it += 2;err
	//双向迭代器
}
void printlist(list& mylist)
{
	for (list::iterator it = mylist.begin(); it != mylist.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
void test02()
{
	listmylist(10,6);
	listmylist2(++mylist.begin(), --mylist.end());
	printlist(mylist2);
}
//元素的插入和删除
bool myfunc(int val)
{
	return val > 300;
}
void test03()
{
	listmylist;
	mylist.push_back(10);
	mylist.push_back(20);
	mylist.push_front(100);
	mylist.push_front(200);
	printlist(mylist);
	vectorv;
	v.push_back(1000);
	v.push_back(2000);
	mylist.insert(mylist.begin(), v.begin(), v. end());
	printlist(mylist);
	mylist.remove(200);
	printlist(mylist);
	//删除大于三百的数
	mylist.remove_if(myfunc);
	printlist(mylist);

}
//大小
void test04()
{
	listmylist;
	for (int i = 0; i < 5; i++)
	{
		mylist.push_back(i + 1);
	}
	cout << "size:" << mylist.size() << endl;
	if (!mylist.empty() )
	{
		cout << "不空" << endl;
	}
	else
	{
		cout << " 空" << endl;
	}
	mylist.resize(3);
	printlist(mylist);
}
//赋值,数据存放
void test05()
{
	listmylist;
	mylist.assign(10, 10);
	printlist(mylist);
	cout << mylist.front() << endl;
	cout << mylist.back() << endl;
	listmylist2;
	for (int i = 0; i < 5; i++)
	{
		mylist2.push_back(i + 1);
	}
	mylist2.swap(mylist);
	printlist(mylist2);
	printlist(mylist);
}
//反转和排序
bool mysort(int val1, int val2)
{
	return val1 > val2;
}
void test06()
{
	listmylist;
	for (int i = 0; i < 5; i++)
	{
		mylist.push_back(i + 10);
	}
	printlist(mylist);
	mylist.reverse();
	printlist(mylist);
	//list容器不能使用sort算法
	mylist.sort(mysort);
	printlist(mylist);
	//默认升序
	mylist.sort();
	printlist(mylist);

}
int main()
{
	test06();
	system("pause");
	return EXIT_SUCCESS;
}

你可能感兴趣的:(c++,list,开发语言)