I am implementing a container class that ensures uniqueness among the elements and restricts insertion and deletion to only the end only. Somewhat like a stack of unique elements or an ordered set with functions like push and pop. And it must also have a fixed maximum size.
template <class T, int max_size>
class FixedSizedUniqueStack
{
std::vector<T> m_vec;
std::unordered_set<T> m_uset;
public:
FixedSizedUniqueStack():m_vec(max_size),m_uset(){}
bool push(T x)
{
bool success = true;
if( m_uset.insert(x).second ) m_vec.push_back(x);
else success = false;
return success;
}
void pop()
{
if(m_vec.size() > 0)
{
m_uset.erase(m_vec.back());
m_vec.pop_back();
}
}
T back()
{
return m_vec.back();
}
};