当前位置:网站首页>Implement a fixed capacity stack
Implement a fixed capacity stack
2022-07-05 05:35:00 【Raise items】
List of articles
Constant volume string stack
Consider implementing a Fixed capacity The stack , This stack can only store character string .
Realization function
fix_string_stack.h
#ifndef FIX_STRING_STACK
#define FIX_STRING_STACK
#include <vector>
#include <string>
using std::vector;
using std::string;
class FixStringStack {
public:
FixStringStack(size_t n);
void Push(const string &str);
string Pop();
bool IsEmpty() const;
size_t Size() const;
private:
vector<string> vStr;
size_t N;
};
#endif
fix_string_stack.cpp
#include "fix_string_stack.h"
FixStringStack::FixStringStack(size_t n) : vStr(n), N(0) {
}
void FixStringStack::Push(const string &str)
{
vStr[N++] = str;
}
string FixStringStack::Pop()
{
return vStr[--N];
}
bool FixStringStack::IsEmpty() const
{
return N == 0;
}
size_t FixStringStack::Size() const
{
return N;
}
Write tests
fix_string_stack_test.h
#ifndef FIX_STRING_TEST_H
#define FIX_STRING_TEST_H
void FixStringStackTest();
#endif
fix_string_stack_test.cpp
#include "fix_string_stack.h"
#include <iostream>
#include <string>
void FixStringStackTest()
{
FixStringStack st(100);
std::string str;
while (std::cin >> str) {
if (str != string("-")) {
st.Push(str);
} else if (!st.IsEmpty()) {
std::cout << st.Pop() << std::endl;
}
}
std::cout << st.Size() << std::endl;
}
function
main.cpp
#include "fix_string_stack_test.h"
int main()
{
FixStringStackTest();
return 0;
}
Running results :
Fixed volume stack advantage Every time Push and Pop Required for operation Time is independent of the length of the stack .
FixStringStack The disadvantages are also obvious :(1) Can only store string object .(2) Before use, you must Estimate the maximum capacity of the stack , It may cause Memory waste .(3)Push Time detection Is the stack full .
Let's ignore vector The ability to dynamically allocate memory , Think of it as an ordinary array .
Constant volume stack
For the above shortcomings , Use the following method to optimize , And try to ensure that its advantages are still .(1) Generic Programming .(2) Time dynamic Allocate memory .
Realization
fix_stack.h
#ifndef FIX_STACK
#define FIX_STACK
#include <cstddef>
template <typename Item>
class FixStack {
public:
FixStack(size_t cap);
~FixStack();
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>
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
C++ in Templates The definition and implementation of classes must be written in The same In the document .
Dynamically allocate memory over time :Push when , If the memory for saving data is full , Just Reallocate current capacity 2 Times more memory , And copy the existing data into the new memory .
Destructor : When the stack object dies , Free the heap memory it requested , Avoid memory leaks .
test
fix_stack_test.h
#ifndef FIX_STACK_TEST
#define FIX_STACK_TEST
void FixStackTest();
#endif
fix_stack_test.cpp
#include "fix_stack.h"
#include <string>
#include <iostream>
void FixStackTest()
{
FixStack<std::string> st(1);
std::string str;
while (std::cin >> str) {
if (str != std::string("-")) {
st.Push(str);
} else if (!st.IsEmpty()) {
std::cout << st.Pop() << std::endl;
}
}
std::cout << st.Size() << std::endl;
}
function
main.cpp
#include "fix_stack_test.h"
int main()
{
FixStackTest();
return 0;
}
Running results :
边栏推荐
- Yolov5 adds attention mechanism
- Kubedm series-00-overview
- Graduation project of game mall
- EOJ 2021.10 E. XOR tree
- Software test -- 0 sequence
- Reader writer model
- Daily question - longest substring without repeated characters
- [to be continued] [UE4 notes] L3 import resources and project migration
- A preliminary study of sdei - see the essence through transactions
- Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
猜你喜欢
![[jailhouse article] jailhouse hypervisor](/img/f4/4809b236067d3007fa5835bbfe5f48.png)
[jailhouse article] jailhouse hypervisor

YOLOv5-Shufflenetv2

Yolov5 adds attention mechanism

Pointnet++的改进

sync.Mutex源码解读

Reader writer model

Sword finger offer 06 Print linked list from beginning to end

【Jailhouse 文章】Jailhouse Hypervisor
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces

剑指 Offer 05. 替换空格
随机推荐
How can the Solon framework easily obtain the response time of each request?
Haut OJ 1221: a tired day
[binary search] 34 Find the first and last positions of elements in a sorted array
CF1637E Best Pair
Haut OJ 1352: string of choice
[jailhouse article] performance measurements for hypervisors on embedded ARM processors
Sword finger offer 06 Print linked list from beginning to end
Educational Codeforces Round 107 (Rated for Div. 2) E. Colorings and Dominoes
【Jailhouse 文章】Look Mum, no VM Exits
Pointnet++的改进
Improvement of pointnet++
使用Electron开发桌面应用
Remote upgrade afraid of cutting beard? Explain FOTA safety upgrade in detail
挂起等待锁 vs 自旋锁(两者的使用场合)
【实战技能】非技术背景经理的技术管理
【Jailhouse 文章】Performance measurements for hypervisors on embedded ARM processors
[binary search] 69 Square root of X
[article de jailhouse] jailhouse hypervisor
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Configuration and startup of kubedm series-02-kubelet