实验目的:掌握
map
、
set
、
vector
、
list
、
deque
等容器的使用
实验内容:定义一个容器存储学生的学号、成绩、电话等,对该容器实现增加、查找、删
除、调用等功能。并实现以上容器的增删改(查)操作。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char* argv[])
{
vector<string>student;
//在学生李明后面加入学生李强
student.push_back("liqiang");
student.push_back("80");
student.push_back("17800000000");
//在开头加入学生张华
student.insert(student.begin(), "zhanghua");
//删除尾部一个元素
student.pop_back();
//删除任意一个元素
student.erase("liqiang");
//迭代器查找元素
vector<string>::iterator iter = std::find(student.begin(), student.end(), "80");
if (iter != student.end())//查找到最后一个元素
{
student.erase(iter);
}
//显示vector中数据
for (int i = 0; i < student.size(); i++) {
cout << student[i] << endl; // 输出1, 3
}
system("pause");
return 0;
}