当前位置:网站首页>Implement an iterative stack

Implement an iterative stack

2022-07-05 05:35:00 Raise items

Range for loop

Collection classes One of the basic operations of data types is Iterate over and process each element in the collection .
C++ Provides Range for Loop statements implement this operation ( Traverse the elements from beginning to end ).

One can be used for Range for loop ( Can the iteration ) The class of the statement must meet the following conditions :
(1) Realization begin() Method . Get directions First element Iterator or pointer to .
(2) Realization end() Method . Get directions The next element of the last element Iterator or pointer to .
(3)begin() and end() The return value of supports ++ operation .

std::vector<int> vi = {
     0, 1, 2, 3, 4, ,5, 6, 7, 8, 9 };
for (auto x : vi) {
    
	std::cout << x << std::endl;
}

Equivalent to

for (auto it = vi.begin(); it != vi.end(); ++it) {
    
	std::cout << *it << std::endl;
}

Iterable

The iterator class is more complex , First use The pointer To illustrate the problem .
iterable.h

#ifndef ITERABLE_H
#define ITERABLE_H

template <typename Item>
class Iterable {
    
public:
    virtual Item *begin() = 0;
    virtual Item *end() = 0;
};

#endif

Variables of primitive pointer type must be able to do ++ Operational .
In this way , All public inheritance Iterable All non abstract classes of can be used in the scope for Loop statement .

Iteratable stack

Use FixStack public inheritance Iterable, Implement an iterative stack .

Realization

fix_stack.h

#ifndef FIX_STACK
#define FIX_STACK

#include <cstddef>
#include "iterable.h"

template <typename Item>
class FixStack : public Iterable<Item> {
    
public:
    FixStack(size_t cap);
    ~FixStack();

    Item *begin() override;
    Item *end() override;

    void Push(const Item &item);
    Item Pop();
    bool IsEmpty() const;
    size_t Size() const;

private:
    void ReSize(size_t max);

    size_t cap_;
    size_t sz_;
    Item *data_;
};

template <typename Item>
FixStack<Item>::FixStack(size_t cap) : cap_(cap), sz_(0)
{
    
    data_ = new Item[cap_];
}

template <typename Item>
FixStack<Item>::~FixStack()
{
    
    delete[] data_;
    data_ = nullptr;
}

template <typename Item>
Item *FixStack<Item>::begin()
{
    
    return data_;
}

template <typename Item>
Item *FixStack<Item>::end()
{
    
    return data_ + sz_;
}

template <typename Item>
void FixStack<Item>::ReSize(size_t max)
{
    
    Item *newData = new Item[max];
    for (size_t i = 0; i < sz_; ++i) {
    
        newData[i] = data_[i];
    }

    data_ = newData;
    cap_ = max;
}

template <typename Item>
void FixStack<Item>::Push(const Item &item)
{
    
    if (sz_ == cap_) {
    
        ReSize( 2 * cap_);
    }
    data_[sz_++] = item;
}

template <typename Item>
Item FixStack<Item>::Pop()
{
    
    return data_[--sz_];
}

template <typename Item>
bool FixStack<Item>::IsEmpty() const
{
    
    return sz_ == 0;
}

template <typename Item>
size_t FixStack<Item>::Size() const
{
    
    return sz_;
}
#endif

test

void FixStackTest2()
{
    
    FixStack<std::string> st(1);
    std::string str;

    while (std::cin >> str) {
    
        st.Push(str);
    }

    for (auto x : st) {
    
        std::cout << x << std::endl;
    }
}

function

int main()
{
    
    FixStackTest2();

    return 0;
}

Running results :
 Insert picture description here

原网站

版权声明
本文为[Raise items]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140621236049.html