Tuesday, 31 March 2015

STL - Container adaptors and the examples in C++

STL - Container adaptors and the examples in Cpp with code examples:

A Container is an object that stores other objects, and that has methods for accessing its elements. In particular, every type that is a model of Container has an associated iterator type that can be used to iterate through the Container's elements.

Each of these "container adaptors" is constructed by taking one of the sequential containers given above and "adapting" its interface to provide the desired behavior.

Stack:
Stack adapts the deque container to provide strict last-in, first-out (LIFO) behavior.

Any sequence supporting operations back, push_back and pop_back can be used to instantiate stack. In particular, vector, list and deque can be used.

Stack
See the below example that helps to implement the stack container
template <class TestContainer>
class stack {

friend bool operator==(const stack<TestContainer>& ocV_Type, const stack<TestContainer>& ocObjContainer);

friend bool operator<(const stack<TestContainer>& ocV_Type, const stack<TestContainer>& ocObjContainer);

public:

typedef TestContainer::V_Type V_Type;

typedef TestContainer::S_Type S_Type;

protected:

TestContainer ocContainerObj;

public:

bool empty () const

  { return ocContainerObj.empty (); }

S_Type size () const

  { return ocContainerObj.size (); }

V_Type& top ()

  { return ocContainerObj.back (); }

const V_Type& top () const

  { return ocContainerObj.back (); }

void push (const V_Type& ocV_Type)

  { ocContainerObj.push_back (ocV_Type); }

void pop ()

  { ocContainerObj.pop_back (); }

};

template <class TestContainer>

bool operator==(const stack<TestContainer>& ocV_Type, const stack<TestContainer>& ocObjContainer)

  { return ocV_Type.ocContainerObj == ocObjContainer.ocContainerObj; }

template <class TestContainer>

bool operator<(const stack<TestContainer>& ocV_Type, const stack<TestContainer>& ocObjContainer)

  { return ocV_Type.ocContainerObj < ocObjContainer.ocContainerObj; }
For example, stack<vector<int> > is an integer stack made out of vector, and stack<deque<char> > is
a character stack made out of deque.

Queue:
Adapts the deque container to provide strict first-in, first-out (FIFO) behavior.
queue
 Any sequence supporting operations front, back, push_back and pop_front can be used to instantiate
queue. In particular, list and deque can be used.

Let us see the sample template code related to queue
template <class TestContainer>

class TestQueue {

friend bool operator==(const TestQueue<TestContainer>& ocV_Type, const TestQueue<TestContainer>& S_Type);

friend bool operator<(const TestQueue<TestContainer>& ocV_Type, const TestQueue<TestContainer>& S_Type);

public:

typedef TestContainer::Value Value;

typedef TestContainer::Size Size;

protected:

TestContainer ocContainer;

public:

bool empty() const

  { return ocContainer.empty (); }

Size size() const

  { return ocContainer.size (); }

Value& front()

  { return ocContainer.front (); }

const Value& front() const

  { return ocContainer.front (); }

Value& back()

  { return ocContainer.back (); }

const Value& back() const

  { return ocContainer.back (); }

void Fun_Push (const Value& ocV_Type)

  { ocContainer.push_back (ocV_Type); }

void Fun_Pop ()

  { ocContainer.pop_front (); }

};

template <class TestContainer>

bool operator==(const TestQueue<TestContainer>& ocV_Type, const TestQueue<TestContainer>& S_Type) {

return ocV_Type.ocContainer == S_Type.ocContainer;

}

template <class TestContainer>

bool operator<(const TestQueue<TestContainer>& ocV_Type, const TestQueue<TestContainer>& S_Type) {

return ocV_Type.ocContainer < S_Type.ocContainer;

}


priority_queue:

The priority queue adapts the vector container to maintain items in a sorted order.

Any sequence with random access iterator and supporting operations front, push_back and pop_back can
be used to instantiate priority_queue. In particular, vector and deque can be used.

template <class TestContainer, class TestVerify = less<TestContainer::value_type> >

class priority_queue

{

public:

typedef TestContainer::value_type value_type;

typedef TestContainer::size_type size_type;

protected:

TestContainer ocContainer;

TestVerify ocTest;

public:

priority_queue (const TestVerify& occompare = TestVerify ()) : ocContainer (), ocTest(occompare)

  {}

template <class InputIterator>

priority_queue (InputIterator ocFirst, InputIterator ocLast,

const TestVerify& occompare = TestVerify ()) : ocContainer (ocFirst, ocLast), ocTest(occompare)

  { make_heap (ocContainer.begin (), ocContainer.end (), ocTest); }

bool empty () const

  { return ocContainer.empty (); }

size_type size () const

  { return ocContainer.size (); }

const value_type& top () const

  { return ocContainer.front (); }

void Fun_Push (const value_type& occompare)

{

ocContainer.push_back (occompare);

push_heap(ocContainer.begin (), ocContainer.end (), ocTest);

}

void pop () {

pop_heap (ocContainer.begin (), ocContainer.end (), ocTest);

ocContainer.pop_back ();

}

};



No comments: