字符串分离splitstring
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(0),cin.tie(0);
return 0;
}
vector<string>SplitString(string s,string c)
{
s=s.substr(1,s.size()-2);//根据具体情况分割
vector<string>res;
string::size_type p1=0;
string::size_type p2=s.find(c,p1);
while(p2!=string::npos)
{
res.push_back(s.substr(p1,p2-p1));
p1=p2+c.size();
p2=s.find(c,p1);
}
if(p1!=string::npos)
{
res.push_back(s.substr(p1));
}
return res;
}
#include "./stdc++.hpp"
using namespace std;
// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
};
int main() {
string line;
while (getline(cin, line)) {
}
return 0;
}
饿汉模式
//饿汉
class CSingleton
{
public:
//3 公有的静态方法访问实例
static CSingleton* get()
{
return mysingleton;
}
//4 内嵌类析构
class CDestory
{
public:
~CDestory()
{
if(CSingleton::mysingleton!=nullptr)
{
delete CSingleton::mysingleton;
mysingleton =nullptr;
cout<<"内嵌类析构函数"<<endl;
}
}
}
static CDestory myDel;//静态对象
private:
static CSingleton* mysingleton;//2 静态成员变量,类内声明,类外初始化
CSingleton()//1 私有化构造函数
{
cout<<"私有化构造函数"<<endl;
}
}
CSingleton* CSingleton::mysingleton=new CSingleton();//静态成员变量,类内声明,类外初始化
CSingleton::CDestory myDel;
懒汉
//懒汉,线程不安全
class CSingleton
{
public:
//3 公有的静态方法访问实例
static CSingleton* get()
{
if(mysingleton==nullptr)
{
mysingleton=new CSingleton()
}
return mysingleton;
}
//4 内嵌类析构
class CDestory
{
public:
~CDestory()
{
if(CSingleton::mysingleton!=nullptr)
{
delete CSingleton::mysingleton;
mysingleton =nullptr;
cout<<"内嵌类析构函数"<<endl;
}
}
}
static CDestory myDel;//静态对象
private:
static CSingleton* mysingleton;//2 静态成员变量,类内声明,类外初始化
CSingleton()//1 私有化构造函数
{
cout<<"私有化构造函数"<<endl;
}
}
CSingleton* CSingleton::mysingleton=nullptr;//静态成员变量,类内声明,类外初始化,但是懒汉先不创建
CSingleton::CDestory myDel;
shared_ptr
#include<iostream>
#include <mutex>
#include <thread>
template<class T>
class shared_ptr
{
public:
shared_ptr(T* ptr):_pPtr(ptr),_pRefcount(new int(1)),_pMutex(new mutex){}
~shared_ptr(){
Release();
}
shared_ptr(const shared_ptr<T>& sp):_pPtr(sp._pPtr),_pRefcount(sp._pRefcount),_pMutex(sp._pMutex){
Addcount();
}
shared_ptr<T>& operator=(const shared_ptr<T>& sp)
{
if(_pPtr!=sp._pPtr)
{
Release();
_pPtr=sp._pPtr;
_pRefcount=sp._pRefcount;
_pMutex=sp._pMutex;
Addcount();
}
return *this;
}
T& operator*()
{
return *_pPtr;
}
T* operator->()
{
return _pPtr;
}
int usecount const()
{
return _pRefcount;
}
T* get()
{
return _pPtr;
}
void Addcount()
{
_pMutex->lock();
++(*_pRefcount);
_pMutex->unlock();
}
private:
void Release()
{
bool deleteflag = false;
_pMutex->lock();
if(--(*_pRefcount)==0)
{
delete _pPtr;
delete _pRefcount;
deleteflag = true;
}
_pMutex->unlock();
if(deleteflag==true)
{
delete _pMutex;
}
}
private:
T* _pPtr;
int* _pRefcount;
mutex* _pMutex;
}