Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include <bits/stdc++.h>
- using namespace std;
- template <typename T>
- struct my_vector{
- int length;
- int tail;
- T* data;
- my_vector() {
- length = 10;
- tail = 0;
- data = (T*) malloc(length * sizeof(T));
- }
- void expand() {
- if (tail == length) {
- length += length;
- data = (T*) realloc(data, length * sizeof(T));
- }
- }
- void push(T x) {
- data[tail] = x;
- tail++;
- expand();
- }
- T pop() {
- tail--;
- return data[tail];
- }
- };
- int main() {
- my_vector<int> a;
- a.push(1);
- a.push(4);
- cout << a.pop() << endl;
- cout << a.pop() << endl;
- my_vector<double> b;
- b.push(3.5);
- b.push(6.6);
- cout << b.pop() << endl;
- cout << b.pop() << endl;
- my_vector<string> c;
- c.push("aaa");
- c.push("bbb");
- cout << c.pop() << endl;
- cout << c.pop() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment