理解容器 list:
1.list 内部结构是线性双向链表,其内部所占内存空间不是连续的,其内部是通过指针将各节点连接起来的,故随机访问元素效率并不高,但是对其插入和删除元素非常快。
2.
通用操作用以下参考程序表现:
#include <iostream>
#include<list>
#include<cstring>
using namespace std;
class student
{
private:
char m_name[20];
int m_age;
public:
student(char *name,int age)
{
strcpy(m_name,name);
m_age=age;
}
char *Getname()
{
return m_name;
}
int Getage();
};
{
private:
char m_name[20];
int m_age;
public:
student(char *name,int age)
{
strcpy(m_name,name);
m_age=age;
}
char *Getname()
{
return m_name;
}
int Getage();
};
int student::Getage()
{
return m_age;
}
int main()
{
list<student> L;
student s1("aa",20);
student s2("bb",21);
student s3("cc",22);
student s4("dd",23);
student s5("ee",24);
//元素入容器;
L.push_back(s1);
L.push_back(s2);
L.push_front(s3);
L.push_front(s4);
L.push_back(s1);
L.push_back(s2);
L.push_front(s3);
L.push_front(s4);
//打印容器内元素信息
for(list<student>::iterator it = L.begin();it != L.end();it++)
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
for(list<student>::iterator it = L.begin();it != L.end();it++)
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
cout<<"*************************************"<<endl;
L.reverse(); //将容器元素反序
for(list<student>::iterator it = L.begin();it != L.end();it++)
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
cout<<"*************************************"<<endl;
L.front()=s5;
L.reverse(); //将容器元素反序
for(list<student>::iterator it = L.begin();it != L.end();it++)
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
cout<<"*************************************"<<endl;
L.front()=s5;
for(list<student>::iterator it = L.begin();it != L.end();it++)
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
{
cout<<"name:"<<it->Getname()<<" "<<"age:"<<it->Getage()<<endl;
}
return 0;
}
1191

被折叠的 条评论
为什么被折叠?



