void TestVector() { vector<studentInfo> students; students.push_back(studentInfo("10021","Zhang san")); students.push_back(studentInfo("10002","Li si")); students.push_back(studentInfo("10021","Wang wu")); students.push_back(studentInfo("10021","Wang liu")); students.push_back(studentInfo("10021","Wu liu")); vector<studentInfo>::iterator it =students.begin(); sort(it,students.end()); outputCont("students sorted",cout,students.begin(),students.end()); students.insert(students.begin()+2,studentInfo("10028","shan chang jiang")); outputCont("添加后",cout,students.begin,students.end()); students.pop_back(); outputCont("删除最后一个",cout,students.begin(),students.end()); students.erase(students.begin()); outputCont("删除第一个",cout,student.begin(),students.end()); } 添加合适的头文件,并改正错误
时间: 2024-02-05 17:13:04 浏览: 74
LPR.zip_lpr_车牌识别 matlab
正确的代码如下所示:
```
#include <iostream>
#include <vector>
#include <algorithm>
#include "studentInfo.h"
using namespace std;
void outputCont(const string& msg, ostream& os, vector<studentInfo>::iterator beg, vector<studentInfo>::iterator end)
{
os << msg << endl;
for (vector<studentInfo>::iterator it = beg; it != end; ++it)
os << *it << endl;
}
void TestVector() {
vector<studentInfo> students;
students.push_back(studentInfo("10021","Zhang san"));
students.push_back(studentInfo("10002","Li si"));
students.push_back(studentInfo("10021","Wang wu"));
students.push_back(studentInfo("10021","Wang liu"));
students.push_back(studentInfo("10021","Wu liu"));
vector<studentInfo>::iterator it =students.begin();
sort(it,students.end());
outputCont("students sorted",cout,students.begin(),students.end());
students.insert(students.begin()+2,studentInfo("10028","shan chang jiang"));
outputCont("添加后",cout,students.begin(),students.end());
students.pop_back();
outputCont("删除最后一个",cout,students.begin(),students.end());
students.erase(students.begin());
outputCont("删除第一个",cout,students.begin(),students.end());
}
int main() {
TestVector();
return 0;
}
```
需要添加的头文件包括 `iostream`、`vector`、`algorithm` 和 `studentInfo.h`。其中 `studentInfo.h` 是自己在另外一个文件中定义的一个结构体,需要根据自己的实际情况进行修改。此外,在 `outputCont` 函数中,需要将 `student.begin()` 修改为 `students.begin()`。
阅读全文