题解(看不懂没事)
2026-03-06 20:05:25
发布于:浙江
7阅读
0回复
0点赞
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
template<typename t>
class vecctor{//定义一个vecctor数组
private://准备
vector<t> a;//准备一个vector数组
//L0
public://功能
void I(const t& value) {//定义I函数,用于输入
a.push_back(value);
}
void O() {//定义O函数,用于删除
a.pop_back();
}
long unsigned int size() {//定义size函数,不加也没事,用于询问有几个参数
return a.size();
}
void coutt() {//定义输出,输出所有元素(L1)
for(int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
}
};
//L2
int main() {
int n;
cin >> n;
vecctor<int> a;
char c;
int x;
for(int i = 1; i <= n; i++) {
cin >> c;
if(c == 'I') {
cin >> x;
a.I(x);
}else{
a.O();
}
}
a.coutt();//L3
}
L1段代码也可以删掉(coutt())然后插在L2可以改成这样:
template<typename U>
ostream& operator<<(ostream &out, const vecctor<U> &obj) {
for (const U& num : obj.a) {
out << num << " ";
}
return out;
}
L3就变成了:
cout << a;
L0加入:
template<typename U>
friend ostream& operator<<(ostream &out, const vecctor<U> &obj);
方案2代码:
#include<iostream>
#include<vector>
#include<climits>
using namespace std;
template<typename t>
class vecctor{
private:
vector<t> a;
template<typename U>
friend ostream& operator<<(ostream &out, const vecctor<U> &obj);
public:
void I(const t& value) {
a.push_back(value);
}
void O() {
a.pop_back();
}
};
template<typename U>
ostream& operator<<(ostream &out, const vecctor<U> &obj) {
for (const U& num : obj.a) {
out << num << " ";
}
return out;
}
int main() {
int n;
cin >> n;
vecctor<int> a;
char c;
int x;
for(int i = 1; i <= n; i++) {
cin >> c;
if(c == 'I') {
cin >> x;
a.I(x);
}else{
a.O();
}
}
cout << a;
}
这里空空如也








有帮助,赞一个