Skip to content

Instantly share code, notes, and snippets.

@KIMBIBLE
Created November 25, 2017 13:56
Show Gist options
  • Save KIMBIBLE/1017a780e88564a8bb7a182afa51d84b to your computer and use it in GitHub Desktop.
Save KIMBIBLE/1017a780e88564a8bb7a182afa51d84b to your computer and use it in GitHub Desktop.
STL> Vector Example1
#include <iostream>
#include <vector>
#include <stdio.h>
using namespace std;
template <typename T>
class Stack
{
public:
Stack() { Clear(); }
void Clear();
size_t Count();
bool IsEmpty();
void Push(T data);
bool Pop(T *dataPtr);
void Print();
private:
vector<T> m_Datas;
};
int main()
{
Stack<int> *IntStack = new Stack<int>;
IntStack->Push(10);
IntStack->Push(20);
IntStack->Push(30);
IntStack->Print();
int value = 0;
IntStack->Pop(&value);
cout << "STACK POP : " << value << endl;
IntStack->Print();
IntStack->Clear();
IntStack->Print();
delete IntStack;
getchar();
}
template <typename T>
void Stack<T>::Clear()
{
if (m_Datas.empty() == false)
{
m_Datas.clear();
}
}
template <typename T>
size_t Stack<T>::Count()
{
return m_Datas.size();
}
template <typename T>
bool Stack<T>::IsEmpty()
{
return m_Datas.empty();
}
template <typename T>
void Stack<T>::Push(T data)
{
m_Datas.push_back(data);
}
template <typename T>
bool Stack<T>::Pop(T* dataPtr)
{
if (IsEmpty() == true)
return false;
memcpy(dataPtr, &m_Datas.back(), sizeof(T));
m_Datas.pop_back();
return true;
}
template <typename T>
void Stack<T>::Print()
{
size_t stackSize = this->Count();
if (stackSize == 0)
{
cout << "Empty Stack!!" << endl;
}
for (int i = 0; i < stackSize; i++)
{
cout << "STACK[" << i << "] : " << m_Datas[i] << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment