#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
using namespace std;
//字符串分割
void SplitString(const std::string &s, std::vector<std::string> &vs, const std::string &patten)
{
std::string::size_type pos1, pos2;
pos2 = s.find(patten);
pos1 = 0;
while (std::string::npos != pos2)
{
vs.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + patten.size();
pos2 = s.find(patten, pos1);
}
if (pos1 != s.length())
vs.push_back(s.substr(pos1));
}
//单例模式
class Singleton
{
public:
~Singleton() {
std::cout << "destructor called!" << std::endl; }
Singleton(const Singleton &) = delete;
Singleton &operator=(const Singleton &) = delete;
static Singleton &get_instance()
{
static Singleton instance;
return instance;
}
int main()
{
Singleton &instance_1 = Singleton::get_instance();
Singleton &instance_2 = Singleton::get_instance();
return 0;
}
private:
Singleton() {
std::cout << "constructor called!" << std::endl; }
};
//使用数组实现栈
class mystack
{
private:
typedef int ElementType; /*栈元素类型*/
#define SUCCESS 1
#define FAILURE -1
#define STACK_SIZE 64 /*栈大小*/
#define TOP_OF_STACK -1 /*栈顶位置*/
int topOfStack ; /*记录栈顶位置*/
ElementType stack[STACK_SIZE]; /*栈数组,也可以使用动态数组实现*/
public:
/*构造函数*/
mystack()
{
topOfStack = TOP_OF_STACK;//初始化栈顶
}
/*入栈,0表示成功,-1表示出错*/
int push(ElementType value)
{
if(this->full())
return FAILURE;
/*先增加topOfStack,再赋值*/
topOfStack++;
stack[topOfStack] = value;
return SUCCESS;
}
/*出栈*/
int pop()
{
/*首先判断栈是否为空*/
if(this->empty())
return FAILURE;
topOfStack--;
return SUCCESS;
}
/*访问栈顶元素*/
ElementType top()
{
/*首先判断栈是否为空*/
if(this->empty()){
std::cerr << "stack is empty! can't get top element" << '\n';
return FAILURE;
}
ElementType value = stack[topOfStack];
return value;
}
/*判断栈是否已满,满返回1,未满返回0*/
int full()
{
return topOfStack == STACK_SIZE - 1;
}
/*判断栈是否为空,空返回1,非空返回0*/
int empty()
{
return topOfStack == - 1;
}
};
//使用数组实现队列(实际使用循环队列)
class myqueue
{
private:
typedef int ElementType;
#define MAX_SIZE 5
#define SUCCESS 1
#define FAILURE -1
/*定义队列结构*/
int front; //队头位置
int rear; //队尾位置
ElementType queueArr[MAX_SIZE]={
};//队列数组
public:
myqueue()
{
front = 1;
rear = 0;
}
/*判断队列是否已满*/
bool full()
{
if((rear &
mylib
最新推荐文章于 2024-07-22 09:38:37 发布
本文详细介绍了自定义的C++算法库mylib,涵盖了其核心功能、使用方法及优化策略,旨在帮助开发者提高算法实现效率。
摘要由CSDN通过智能技术生成