C++11容器emplace方法原理剖析 单例模式 简单工厂 工厂方法 线程安全的懒汉单例模式
·
//#include <vector>
#include <map>
template <typename T>
class MyAllocator {
public:
T* allocate(size_t size) {
return (T*)malloc(sizeof(T) * size);
}
template <typename... Types>
void construct(T* p, Types&&... args) {
new (p)T(std::forward<Types>(args)...);
}
void deallocate(T* p) {
free(p);
}
void destroy(T* p) {
p->~T();
}
};
template <typename T,typename Alloc=MyAllocator<T>>
class vector {
public:
vector()
:vec_(nullptr)
, size_(0)
, idx_(0)
{}
void reserve(size_t size) {
vec_ = allocator_.allocate(size);
}
~vector() {
for (int i = 0; i < idx_; i++)
{
allocator_.destroy(vec_ + i);
}
allocator_.deallocate(vec_ );
}
/*void push_back(const T& val) {
allocator_.construct(vec_ + idx_, val);
idx_;
}
void push_back(T&& val) {
allocator_.construct(vec_ + idx_, std::move(val));
idx_++;
}*/
template <typename...Type>
void push_back(Type...args) {
allocator_.construct(vec_ + idx_, std::forward<Type>(args)...);
idx_++;
}
template<typename... Type>
void emplace_back(Type&&... args) {//类型完美转发 使类型保持不变
allocator_.construct(vec_ + idx_, std::forward<Type>(args)...);
idx_++;
}
private:
T* vec_;
int size_;
int idx_;
Alloc allocator_;
};
class Test {
public:
Test(int a) { cout << "Test(int a)" << endl; }
Test(int a, int b) { cout << "Test(int a,int b)" << endl; }
Test(const Test& src) { cout << "Test(const Test& src)" << endl; }
Test(Test&& src) { cout << "Test(Test&& src)" << endl; }
~Test() { cout << "~Test()" << endl; }
};
int main() {
vector<Test> v;
v.reserve(100);
Test t1(10);
//传入对象没有变
cout << "======================" << endl;
v.push_back(t1);
v.emplace_back(t1);
cout << "======================" << endl;
v.push_back(Test(20));
v.emplace_back(Test(20));
cout << "======================" << endl;
//给emplace传入构造函数所需参数 直接在容器底层调用构造函数即可
v.emplace_back(10);
v.emplace_back(10,20);
cout << "======================" << endl;
map<int, string> m;
m.insert(make_pair(1, "zhang san "));
m.insert({ 2,"zhangsan" });
m.emplace(4, "zhang san");
return 0;
}
//单例模式 日志模式 数据库模式
class Singleton {//饿汉式单例模式
public:
static Singleton* getInstance() {
return &instance;
}
private:
Singleton() { cout << "Singleton()" << endl; }
static Singleton instance;
Singleton(const Singleton& src) = delete;
Singleton& operator=(const Singleton& src) = delete;
};
Singleton Singleton::instance;
int main() {
Singleton* p1 = Singleton::getInstance();
Singleton* p2 = Singleton::getInstance();
Singleton* p3 = Singleton::getInstance();
return 0;
}
class Singleton {//懒汉式单例模式
public:
static Singleton* getInstance() {
if (instance == nullptr) {
instance = new Singleton();
}
return instance;
}
private:
Singleton() { cout << "Singleton()" << endl; }
static Singleton* instance;
Singleton(const Singleton& src) = delete;
Singleton& operator()(const Singleton& src) = delete;
};
Singleton* Singleton::instance=nullptr;
int main() {
Singleton* p1 = Singleton::getInstance();
Singleton* p2 = Singleton::getInstance();
Singleton* p3 = Singleton::getInstance();
return 0;
}
#include <thread>
#include <mutex>
std::mutex mtx;
class Singleton {//懒汉式线程安全
public:
static Singleton* getInstance() {
if (instance == nullptr) {
lock_guard<std::mutex> guard(mtx);
if (instance == nullptr) {
instance = new Singleton();
}
}
return instance;
}
private:
static Singleton* instance;
Singleton() { cout << "Singleton()" << endl; }
Singleton(const Singleton& src) = delete;
Singleton& operator=(const Singleton& src) = delete;
};
Singleton* Singleton::instance = nullptr;
int main() {
Singleton* p1 = Singleton::getInstance();
Singleton* p2 = Singleton::getInstance();
return 0;
}
class Singleton {
public:
static Singleton* getInstance() {
static Singleton instance;
return &instance;
}
private:
Singleton() { cout << "Singleton()" << endl; }
Singleton(const Singleton& src) = delete;
Singleton& operator=(const Singleton& src) = delete;
};
int main() {
Singleton* p1 = Singleton::getInstance();
Singleton* p2 = Singleton::getInstance();
return 0;
}
#include <memory>
#include <string>
class Car {
public:
Car(string name)
:_name(name)
{}
virtual void show() = 0;
protected:
string _name;
};
class Bwm :public Car {
public:
Bwm(string name)
:Car(name)
{}
void show() {
cout << "获得一辆宝马:" << _name << endl;
}
};
class Audi :public Car {
public:
Audi(string name)
:Car(name)
{}
void show() {
cout << "获得一辆奥迪:" << _name << endl;
}
};
//enum CarType{
// BWM,
// AUDI
//};
//class Factory {
//public:
// template <typename CarType>
// Car* createCar(CarType ct) {
// switch (ct) {
// case BWM:
// return new Bwm("x1");
// break;
// case AUDI:
// return new Audi("A9");
// break;
// default:
// cout << "数字无效" << endl;
// break;
// }
// return nullptr;
// }
//private:
//};
class Factory {
public:
virtual Car* createCar(string name) = 0;
};
class BwmFactory :public Factory {
public:
Car* createCar(string name) {
return new Bwm(name);
}
};
class AudiFactory :public Factory {
public:
Car* createCar(string name) {
return new Audi(name);
}
};
#include <memory>
int main() {
/*Factory* fty=new Factory();
Car* bwm=fty->createCar(BWM);
Car* audi=fty->createCar(AUDI);
bwm->show();
audi->show();
delete fty;
delete bwm;
delete audi;*/
unique_ptr<Factory> bwmfty(new BwmFactory());
unique_ptr<Factory> audifty(new AudiFactory());
unique_ptr<Car> p1(bwmfty->createCar("x2"));
unique_ptr<Car> p2(audifty->createCar("A8"));
p1->show();
p2->show();
return 0;
}
更多推荐



所有评论(0)