当前位置:网站首页>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 :
边栏推荐
- Fried chicken nuggets and fifa22
- Haut OJ 1221: a tired day
- Zzulioj 1673: b: clever characters???
- Sword finger offer 06 Print linked list from beginning to end
- [jailhouse article] jailhouse hypervisor
- Educational codeforces round 109 (rated for Div. 2) C. robot collisions D. armchairs
- CCPC Weihai 2021m eight hundred and ten thousand nine hundred and seventy-five
- Haut OJ 1352: string of choice
- Kubedm series-00-overview
- Demonstration of using Solon auth authentication framework (simpler authentication framework)
猜你喜欢
CF1634 F. Fibonacci Additions
利用HashMap实现简单缓存
Service fusing hystrix
【Jailhouse 文章】Look Mum, no VM Exits
CF1634E Fair Share
[to be continued] [UE4 notes] L1 create and configure items
Yolov5 ajouter un mécanisme d'attention
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
Graduation project of game mall
Little known skills of Task Manager
随机推荐
Binary search basis
On-off and on-off of quality system construction
Pointnet++ learning
二十六、文件系统API(设备在应用间的共享;目录和文件API)
Add level control and logger level control of Solon logging plug-in
[article de jailhouse] jailhouse hypervisor
Sword finger offer 35 Replication of complex linked list
MySQL数据库(一)
YOLOv5-Shufflenetv2
[jailhouse article] look mum, no VM exits
过拟合与正则化
kubeadm系列-00-overview
[depth first search] 695 Maximum area of the island
Graduation project of game mall
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Haut OJ 1316: sister choice buys candy III
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
To be continued] [UE4 notes] L4 object editing
【Jailhouse 文章】Look Mum, no VM Exits
剑指 Offer 05. 替换空格