当前位置:网站首页>Implement an iterative stack
Implement an iterative stack
2022-07-05 05:35:00 【Raise items】
List of articles
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 :
边栏推荐
- 2020ccpc Qinhuangdao J - Kingdom's power
- The number of enclaves
- Personal developed penetration testing tool Satania v1.2 update
- 读者写者模型
- Talking about JVM (frequent interview)
- Yolov5 ajouter un mécanisme d'attention
- Haut OJ 1243: simple mathematical problems
- Sword finger offer 58 - ii Rotate string left
- Developing desktop applications with electron
- 第六章 数据流建模—课后习题
猜你喜欢
游戏商城毕业设计
Service fusing hystrix
object serialization
Yolov5 adds attention mechanism
【Jailhouse 文章】Look Mum, no VM Exits
YOLOv5添加注意力机制
剑指 Offer 09. 用两个栈实现队列
Analysis of backdoor vulnerability in remote code execution penetration test / / phpstudy of national game title of national secondary vocational network security B module
剑指 Offer 06.从头到尾打印链表
浅谈JVM(面试常考)
随机推荐
Introduction to memory layout of FVP and Juno platforms
利用HashMap实现简单缓存
Introduction to tools in TF-A
剑指 Offer 53 - I. 在排序数组中查找数字 I
In this indifferent world, light crying
第六章 数据流建模—课后习题
Light a light with stm32
PC寄存器
全国中职网络安全B模块之国赛题远程代码执行渗透测试 //PHPstudy的后门漏洞分析
剑指 Offer 06.从头到尾打印链表
[binary search] 69 Square root of X
剑指 Offer 53 - II. 0~n-1中缺失的数字
Alu logic operation unit
On-off and on-off of quality system construction
Cluster script of data warehouse project
卷积神经网络——卷积层
Convolution neural network -- convolution layer
Web APIs DOM node
剑指 Offer 04. 二维数组中的查找
Binary search basis