如果我需要定义一个学生结构体,申明vector向量,用于存放任意个学生的信息(学号成绩啥的),然后根据学号排序,这该如何实现
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
using namespace std;
struct Student
{
string Name; // 名字
int Id; // 学号
int Score; // 成绩
};
int main()
{
cout << "输入学生的名字、学号和成绩 (用空格间隔开来,输入 exit 结束输入):" << endl;
vector<Student> students;
while (true)
{
Student student;
cin >> student.Name;
if (student.Name == "exit") break;
cin >> student.Id >> student.Score;
students.emplace_back(student);
}
sort(students.begin(), students.end(), [](const Student &a, const Student &b) { return a.Id < b.Id; });
cout << "名字\t学号\t成绩" << endl;
for (const auto &student : students)
{
cout << left << setw(8) << student.Name;
cout << left << setw(8) << student.Id;
cout << left << setw(8) << student.Score;
cout << endl;
}
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id; // 学号
int score; // 成绩
};
vector<Student> students; // 声明一个空的学生向量
// 按照学号进行排序
void sortStudentsByID(vector<Student>& students) {
sort(students.begin(), students.end(), greater<string>()); // 使用 greater<string> 函数对象进行排序
}
int main() {
// 添加学生信息
students.push_back(Student{"1001", 90});
students.push_back(Student{"1002", 80});
students.push_back(Student{"1003", 70});
students.push_back(Student{"1004", 80});
// 按照学号进行排序
sortStudentsByID(students);
// 输出排序后的学生信息
for (auto student : students) {
cout << "学号:" << student.id << ", 成绩:" << student.score << endl;
}
return 0;
}
c++中常用的vector容器作为参数时,有三种传参方式,分别如下:
function1(vector vec),传值
function2(vector &vec),传引用
function3(vector *vec),传指针
注意,三种方式分别有对应的const形式,不在此讨论。
三种方式对应的调用形式分别为:
function1(vec),传入值
function2(vec),传入引用
function3(&vec),传入地址
三种方式的效果分别为:
会发生拷贝构造——不能修改原始实际的vector元素的值,这一点与普通数组名的传值参数不一样,数组名的参数传递时可以修改到实际数组元素值的;
不会发生拷贝构造
不会发生拷贝构造
例子:在DFS算法中使用visited标记数组
#include<iostream>
#include<vector>
using namespace std;
int n, e, s;
void DFS(vector<vector<int>>G, int N,vector<int>&V) //此处vector不能设置为传值参数!!否则在递归的过程中之前的标记改动无效!!
{
V[N] = 1;
if (N != s)cout << " ";
cout << N;
for (int v = 0; v < n;v++)if (!V[v]&&G[N][v])DFS(G, v, V);
}
int main() {
int m;
cin >> m;
while (m--) {
cin >> n >> e >> s;
int u, v;
vector<vector<int>>graph(n,vector<int>(n,0));
vector<int>visited(n, 0);
while (e--) {
cin >> u >> v;
graph[u][v]=1, graph[v][u]=1;
}
DFS(graph, s,visited);
cout << endl;
}
}
学生结构体可以定义如下:
struct Student { int id; // 学号 float score; // 成绩 };
使用vector存储学生信息可以声明一个vector容器:
vector students;
然后可以使用push_back()函数向容器中添加元素,例如:
students.push_back({10001, 98.5});
根据学号对学生进行排序,可以使用sort()函数,并定义一个比较函数作为sort()的第三个参数,例如:
bool cmp(const Student& s1, const Student& s2) { return s1.id < s2.id; }
sort(students.begin(), students.end(), cmp);
完整代码如下:
using namespace std;
struct Student { int id; float score; };
bool cmp(const Student& s1, const Student& s2) { return s1.id < s2.id; }
int main() { vector students; students.push_back({10002, 85}); students.push_back({10001, 98.5}); students.push_back({10003, 76.5});
sort(students.begin(), students.end(), cmp);
for (auto s : students) {
cout << "id: " << s.id << ", score: " << s.score << endl;
}
return 0;
}