当前位置:网站首页>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 :
边栏推荐
- Sword finger offer 35 Replication of complex linked list
- Little known skills of Task Manager
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
- 搭建完数据库和网站后.打开app测试时候显示服务器正在维护.
- Control Unit 控制部件
- Acwing 4301. Truncated sequence
- Haut OJ 1241: League activities of class XXX
- [to be continued] [UE4 notes] L1 create and configure items
- Codeforces Round #732 (Div. 2) D. AquaMoon and Chess
- Solution to the palindrome string (Luogu p5041 haoi2009)
猜你喜欢
随机推荐
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
A new micro ORM open source framework
[to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Haut OJ 1352: string of choice
Configuration and startup of kubedm series-02-kubelet
Acwing 4300. Two operations
AtCoder Grand Contest 013 E - Placing Squares
Little known skills of Task Manager
In this indifferent world, light crying
常见的最优化方法
Annotation and reflection
kubeadm系列-00-overview
How can the Solon framework easily obtain the response time of each request?
Personal developed penetration testing tool Satania v1.2 update
Developing desktop applications with electron
从Dijkstra的图灵奖演讲论科技创业者特点
object serialization
CF1634E Fair Share
Haut OJ 1241: League activities of class XXX