当前位置:网站首页>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 :
边栏推荐
- YOLOv5添加注意力機制
- Acwing 4301. Truncated sequence
- R语言【数据集的导入导出】
- Sword finger offer 58 - ii Rotate string left
- [depth first search] 695 Maximum area of the island
- Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
- Web APIs DOM node
- 用STM32点个灯
- kubeadm系列-02-kubelet的配置和启动
- Haut OJ 2021 freshmen week II reflection summary
猜你喜欢
【Jailhouse 文章】Look Mum, no VM Exits
Using HashMap to realize simple cache
[to be continued] [depth first search] 547 Number of provinces
YOLOv5-Shufflenetv2
SAP method of modifying system table data
【Jailhouse 文章】Jailhouse Hypervisor
剑指 Offer 05. 替换空格
Fried chicken nuggets and fifa22
Palindrome (csp-s-2021-palin) solution
Educational Codeforces Round 116 (Rated for Div. 2) E. Arena
随机推荐
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
Over fitting and regularization
读者写者模型
On-off and on-off of quality system construction
Developing desktop applications with electron
Little known skills of Task Manager
Web APIs DOM node
Hang wait lock vs spin lock (where both are used)
To be continued] [UE4 notes] L4 object editing
Sword finger offer 35 Replication of complex linked list
kubeadm系列-00-overview
Codeforces Round #716 (Div. 2) D. Cut and Stick
Use of room database
Software test -- 0 sequence
[jailhouse article] jailhouse hypervisor
第六章 数据流建模—课后习题
Daily question - Search two-dimensional matrix PS two-dimensional array search
Time complexity and space complexity
Detailed explanation of expression (csp-j 2021 expr) topic
Solution to the palindrome string (Luogu p5041 haoi2009)