C++模板库list

#include<stdio.h>
#include<list>
using namespace std;
int main()
{
	list<int> mylist(4);
	mylist.clear();
	mylist.push_back(1);
	mylist.push_back(2);
	mylist.push_back(3);
	mylist.push_back(4);
	mylist.push_back(6);
	mylist.push_back(7);

	for (list<int>::iterator iter = mylist.begin(); iter!=mylist.end(); iter++)
	{
		int& value = *iter;  //删除操作
		if (value == 3)
		{
			mylist.erase(iter); 
			break;
		}
	}
	for (list<int>::iterator iter = mylist.begin(); iter != mylist.end(); iter++)
	{
		int& value = *iter;  //插入操作,在判断的前面插入
		if (value == 6)
		{
			mylist.insert(iter,5);
		}
	}

	for (list<int>::iterator iter = mylist.begin(); iter != mylist.end(); iter++) //迭代器遍历
	{
		int& value = *iter;
		printf("%d\n", value);
	}
	return 0;

}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<list>
using namespace std;

class Student
{
public:
	int id;
	char name[32];
	Student()
	{
	}
	Student(int id, char* str)
	{
		this->id = id;
		strcpy(this->name ,str);
	}

};
int main()
{
	list<Student> stulist(4);
	stulist.clear();
	Student stu(2017001, "xiaoming");
	stulist.push_back(stu);

	Student stu2;
	stu2.id = 2017002;
	strcpy(stu2.name, "xiaohong");
	stulist.push_front(stu2);

	for (list<Student>::iterator iter = stulist.begin(); iter != stulist.end(); iter++)
	{
		Student& tem = *iter;
		printf("%d,%s\n", (*iter).id, tem.name);
	}


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值