unique_ptr
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
struct Person {
~Person() {
cout << "delete cotr" << endl;
}
Person() {}
Person(string name) { cout << "name is " << name << endl; }
};
void func1() {
unique_ptr<Person>p1(new Person);
unique_ptr<Person>p2 = move(p1);
if (!p1)cout << "p1 is nullptr" << endl;
if (p2)cout << "p2 is unique" << endl;
p2.reset(new Person);
}
void func2() {
auto ptr = make_unique<Person>("hj");
}
template <typename T>
struct Node {
T data;
unique_ptr<Node<T>> next;
~Node() {
cout << "~Node" << endl;
}
};
template <typename T>
class Link {
public:
void front(const T& data) {
auto node = make_unique<Node<T>>();
node->data = data;
node->next = move(head_.next);
head_.next = move(node);
}
void print() {
Node<T>*node = head_.next.get();
while (node) {
cout << node->data << " ";
node = node->next.get();
}
cout << endl;
}
private:
Node<T>head_;
};
void func3() {
Link<int>l;
for (int val : {1, 2, 3, 4, 5}) {
l.front(val);
}
l.print();
}
int main() {
func1();
}
shared_ptr
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
int main()
{
std::shared_ptr<int> p1 = std::make_shared<int>();
*p1 = 78;
std::cout << "p1 = " << *p1 << std::endl;
std::cout << "p1 Reference count = " << p1.use_count() << std::endl;
std::shared_ptr<int> p2(p1);
std::cout << "p2 Reference count = " << p2.use_count() << std::endl;
std::cout << "p1 Reference count = " << p1.use_count() << std::endl;
if (p1 == p2) {
std::cout << "p1 and p2 are pointing to same pointer\n";
}
std::cout << "Reset p1 " << std::endl;
p1.reset();
std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
p1.reset(new int(11));
std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
p1 = nullptr;
std::cout << "p1 Reference Count = " << p1.use_count() << std::endl;
if (!p1) {
std::cout << "p1 is NULL" << std::endl;
}
return 0;
}
给shared_ptr添加自定义删除器
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
struct Sample
{
Sample() {
std::cout << "Sample\n";
}
~Sample() {
std::cout << "~Sample\n";
}
};
void deleter(Sample * x)
{
std::cout << "Custom Deleter\n";
delete[] x;
}
int main()
{
std::shared_ptr<Sample> p3(new Sample[3], deleter);
return 0;
}
使用Lambda 表达式 / 函数对象作为删除器
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
struct Sample
{
Sample() {
std::cout << "Sample\n";
}
~Sample() {
std::cout << "~Sample\n";
}
};
class Deleter
{
public:
void operator() (Sample * x) {
std::cout << "DELETER FUNCTION CALLED\n";
delete[] x;
}
};
int main()
{
std::shared_ptr<Sample> p3(new Sample[3], Deleter());
std::shared_ptr<Sample> p4(new Sample[3], [](Sample * x) {
std::cout << "DELETER FUNCTION CALLED\n";
delete[] x;
});
}
与普通指针相比,shared_ptr仅提供-> 、*和==运算符,没有+、-、++、–、[]等运算符
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
struct Sample {
void dummyFunction() {
std::cout << "dummyFunction" << std::endl;
}
};
int main()
{
std::shared_ptr<Sample> ptr = std::make_shared<Sample>();
(*ptr).dummyFunction();
ptr->dummyFunction();
std::shared_ptr<Sample> ptr2(ptr);
if (ptr == ptr2)
std::cout << "ptr and ptr2 are equal" << std::endl;
return 0;
}
不要使用同一个原始指针构造 shared_ptr
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
int main()
{
int *num = new int(23);
std::shared_ptr<int> p1(num);
std::shared_ptr<int> p2(p1);
std::shared_ptr<int> p3(num);
std::cout << "p1 Reference = " << p1.use_count() << std::endl;
std::cout << "p2 Reference = " << p2.use_count() << std::endl;
std::cout << "p3 Reference = " << p3.use_count() << std::endl;
return 0;
}
不要用栈中的指针构造 shared_ptr 对象
#include <iostream>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <string>
#include <memory>
using namespace std;
int main()
{
int x = 12;
std::shared_ptr<int> ptr(&x);
return 0;
}