容器 迭代器

来源:互联网 发布:兵棋推演软件 编辑:程序博客网 时间:2024/06/16 05:17

STL包括:   容器(数据结构)  迭代器(遍历数据)    算法

顺序容器:  vector(矢量)  list(链表)  deque(双端队列)

关联容器:set(集合) multist  ,  map(映射),multimap

迭代器:类似于指针 用来访问容器中的单个数据项

迭代器由类iterator来声明。

#include<iostream>

#include <algorithm>
#include <vector>
using namespace std;
struct student
{
int numer;
char name[20];
float score;


};
bool comper(student s1, student s2)
{
return s1.score < s2.score;
}
int main()
{
student s1{ 10000,"张三",78.9 }, s2{ 10001,"王武",98.0 }, s3{ 10002,"李四",78.2 }, \
s4{ 10003,"李思思",99.0 }, s5{ 10004,"焦豆豆",60.0 }, s6{10005,"张飞",88.4};
vector<student> vt;
vt.push_back(s1);
vt.push_back(s2);
vt.push_back(s3);
vector<student>::iterator vi;
vi = vt.begin();
vi = vt.insert(vi,s4);
vi = vt.insert(vi,s5);
    vt.insert(vi,s6);
for (vi = vt.begin(); vi != vt.end(); vi++)
cout << vi->numer << '\t' << vi->name <<'\t'<<vi->score << endl;
cout << "----------排序后-------------" << endl;
sort(vt.begin(),vt.end(),comper);
for (vi = vt.begin(); vi != vt.end(); vi++)
cout << vi->numer << '\t' << vi->name << '\t' << vi->score << endl;




system("pause");
return 0;
}
原创粉丝点击