#include <iostream> using namespace std; template<class T> class List { public: List() :pFirst(nullptr) {} //构造函数 void Add(T& val) { Node* pNode = new Node; pNode->pT = &val; pNode->pNext = pFirst; pFirst = pNode; } //在Link表头添加新结点 void Remove(T& val) { Node* pNode = pFirst; Node* pPrev = nullptr; while (pNode) { if ((pNode->pT) == val) { if (pPrev) { pPrev->pNext = pNode->pNext; } else { pFirst = pNode->pNext; } delete pNode; return; } pPrev = pNode; pNode = pNode->pNext; } } //在Link中删除含有特定值的元素 T Find(T& val) { Node* pNode = pFirst; while (pNode) { if ((pNode->pT) == val) { return pNode->pT; } pNode = pNode->pNext; } return nullptr; } //查找含有特定值的结点 void PrintList() { Node pNode = pFirst; while (pNode) { std::cout << (pNode->pT) << std::endl; pNode = pNode->pNext; } } //打印输出整个链表 ~List() { Node pNode = pFirst; while (pNode) { Node* pNext = pNode->pNext; delete pNode; pNode = pNext; } } protected: struct Node { Node* pNext; T* pT; }; Node* pFirst; //链首结点指针 }; class Student { private: std::string name_; int id_; public: Student(const std::string& name, int id) :name_(name), id_(id) {} bool operator==(const Student& other) const { return id_ == other.id_; } friend std::ostream& operator<<(std::ostream& os, const Student& student); }; std::ostream& operator<<(std::ostream& os, const Student& student) { os << "Name: " << student.name_ << ", ID: " << student.id_; return os; } int main() { List<Student> classList; Student s1("张三", 1001); Student s2("李四", 1002); Student s3("王五", 1003); //添加学生 classList.Add(s1); classList.Add(s2); classList.Add(s3); //打印学生 classList.PrintList(); std::cout << std::endl; //查找学生 Student s4("李四", 1002); Student* pStudent = classList.Find(s4); if (pStudent) { std::cout << "Found student: " << *pStudent << std::endl; } else { std::cout << "Student not found." << std::endl; } std::cout << std::endl; //删除学生 classList.Remove(s2); classList.PrintList(); return 0; }请见查找学生进行完善
时间: 2024-01-15 11:04:05 浏览: 20
在List类中的Find函数中,如果没有找到指定的元素,应该返回一个T类型的默认值,而不是nullptr。因为T类型可以是任何类型,不一定是指针类型。可以在List类模板定义中加入一个默认的模板参数,指定T类型的默认值,例如:
```
template<class T, T defaultValue = T{}> class List {
//...
T Find(T& val) {
Node* pNode = pFirst;
while (pNode) {
if (*(pNode->pT) == val) {
return *(pNode->pT);
}
pNode = pNode->pNext;
}
return defaultValue;
}
//...
};
```
这样,在默认情况下,T类型的默认值为0,如果T是一个指针类型,则T类型的默认值为nullptr。如果需要使用其他类型的默认值,可以通过指定模板参数来实现。
相关问题
#include<iostream> #include<cstdio> using namespace std;是什么意思
#include<iostream> #include<cstdio> using namespace std; 是C++中的预处理指令,用于引入头文件和命名空间。
1. #include<iostream> 是引入iostream头文件,其中包含了输入输出流的定义,例如cout和cin等。
2. #include<cstdio> 是引入cstdio头文件,其中包含了C语言标准输入输出函数的定义,例如printf和scanf等。
3. using namespace std; 是使用std命名空间,std是C++标准库的命名空间,其中包含了很多常用的函数和类。
这些预处理指令的作用是为了在程序中能够使用输入输出流和标准库函数,使得程序更加方便和简洁。
#include<iostream>using namespace std;
这是一个C++的代码片段,它包含了iostream头文件和使用了命名空间std。iostream头文件提供了输入输出流的功能,而using namespace std;语句则是为了方便使用std命名空间中的函数和对象,避免每次都需要写std::。
以下是一个简单的示例代码,展示了如何使用iostream头文件和命名空间std来输出一句话:
```cpp
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
```
这段代码会输出"Hello, World!",endl是一个特殊的控制符,用于在输出流中插入一个换行符。