类模板的初步认识与使用
发布日期:2021-05-10 02:08:12 浏览次数:16 分类:精选文章

本文共 2815 字,大约阅读时间需要 9 分钟。

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������������`main.cpp`������������������������������������������������������������
```cpp
#include "pch.h"
#include
#include "stack1.h"

������������std������������������������������������������������������������������

using namespace std;
_space1::stack
s;

���������������������������������������0���9���10������������������������

for (int i = 0; i < 10; ++i) {
s.push(i);
}

���������������������������������������������������������

int ret = s.top();
cout << "���������������: " << ret << endl;
s.pop();
ret = s.top();
cout << "���������������: " << ret << endl;

���������������������������������������������������������������������������������������������������������������

if (s.empty()) {
cout << "������������������������" << endl;
}

���������������������������������������������������������������������������������������

������������������stack1.h���������������������������������������������������stack������������������������T���

template
class stack {
private:
vector
elems;
public:
void push(t const& elem);
void pop();
t top() const;
bool empty() const;
stack();
stack(stack
const& other);
stack
& operator=(stack
const& other);
};

������������������������������vector������������������������������������������������������������������������������������������������������������������������������������

������������������������������������������������������������������������������������������������������������������������������������

void stack
::pop() {
if (stack
::empty()) {
cout << "���������������������������" << endl;
}
elems.pop_back();
}

������������������������������������������������������������

t stack
::top() const {
if (stack
::empty()) {
cout << "���������������������������������������" << endl;
}
return elems.back();
}

���������������������������������������������������������������

���������������������������������������������������������������������������������������

#include "pch.h"
#include
using namespace std;

������������������������������������������������������

������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

 
上一篇:类模板的特化与局部特化
下一篇:函数模板总结

发表评论

最新留言

路过按个爪印,很不错,赞一个!
[***.219.124.196]2025年04月09日 04时33分54秒