Vector-based implementation of adapter stack (sequential stack) in C + + __c++

Source: Internet
Author: User

We've talked about using templates to implement sequential table code implementation
Template sequence table
But in practice, we often need to transform an interface into another interface that the user needs, which requires the adapter in C + + to package the original program. Stacks are one of them. The

Stack follows the rules for LIFO, which are inserted and deleted at the top of the stack and are a special linear sequence table, so we usually build the stack on the basis of the sequential table. Srack does not allow traversal, nor does it provide an iterator.
The following is the code implementation of the stack:

#define _crt_secure_no_warnings 1 #pragma once #include <iostream> #include <string> using namespace std;

    Template<class t> class Vector {public:vector (): _a (NULL), _size (), _capacity () {}
            ~vector () {if (_a) {delete [] _a;
            _a = NULL;
            _size = 0;
        _capacity = 0;
        } void Pushback (const t& x) {checkcapacity ();
        _a[_size] = x;
    _size++;
    } void Popback () {_size--;
        } void Popfront () {for (size_t i = 0; i < _size; i++) {_a[i] = _a[i + 1];
    } _size--;
    BOOL Empty () {return (_size = 0);
    t& back () {return _a[_size-1];  } void Checkcapacity () {if (_size = = _capacity) {size_t newcapacity = _capacity * 2
            + 3;
    t* tmp = new t[newcapacity];        if (_a) {for (size_t i = 0; i < _size; i++) {
                Tmp[i] = _a[i];
            }} delete[] _a;
            _a = tmp;
        _capacity = newcapacity; } void Print () {for (size_t i = 0; i < _size i++) {cout << _a[i] &l
        t;< "";
    } cout << Endl;
    } protected:t* _a;
    size_t _size;
size_t _capacity;

};
    void Funtest () {vector<int> V;
    V.pushback (1);
    V.pushback (2);
    V.pushback (3);
    V.pushback (4);

    V.print ();
    Vector<string> v1; V1.
    Pushback ("11"); V1.
    Pushback ("22"); V1.
    Pushback ("33"); V1.
    Pushback ("44"); V1.
Print (); } Template<class T,class container>//adapter class Stack {public:void Push (const t& x) {_con.
    Pushback (x); } void Pop () {_con.
    Popback ();
       } t& Top () { Return _con.
    Back (); size_t size () {return _con.
    Size (); BOOL Empty () {return _con.
    Empty ();
} Protected:container _con;

};
    void Teststack () {stack<int,vector<int>> s;
    S.push (1);
    S.push (2);
    S.push (3);
        while (!s.empty ()) {cout << s.top () << "";
    S.pop ();
} cout << Endl;
    int main () {//funtest ();
    Teststack ();
    System ("pause\n");
return 0;
 }

The

Execution results are as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.