mylib

本文详细介绍了自定义的C++算法库mylib,涵盖了其核心功能、使用方法及优化策略,旨在帮助开发者提高算法实现效率。
摘要由CSDN通过智能技术生成
#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 &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值