【Smart_Point】动态内存与智能指针
動態(tài)內存
動態(tài)內存使用的三種原因
- 程序不知道自己需要多少對象
- 程序不知道所需對象的準確類型
- 程序需要在多個對線之間共享數(shù)據(jù)
文章目錄
- 動態(tài)內存
- 動態(tài)內存使用的三種原因
- 實例1: Exercise 12.2:
- Write your own version of the StrBlob class including the const versions of front and back.
- test
- Exercise 12.6:
- Exercise 12.7: Redo the previous exercise, this time using shared_ptr.
定義StrBlob類如下Exercise 12.2
StrBlob():data(std::make_shared<vector<string>>()) { }StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) { }
2個構造函數(shù)都是用初始化成員列表來初始化data成員,令data指向一個動態(tài)分配的vector. 默認構造分配一個空的vector,然后接受一個initializer_list的構造函數(shù)通過拷貝列表中的值來初始化vector元素。
如下實例1:所示 check私有工具函數(shù),做索引檢查,處理異常參數(shù)。
StrBlob 只有一個數(shù)據(jù)成員,當StrBlob 發(fā)生拷貝,賦值或者銷毀StrBlob 對象時候,它的shared_ptr成員也會拷貝,賦值或者銷毀。
拷貝一個shared_ptr會遞增其引用計數(shù),當將一個shared_ptr賦值給另外一個shared_ptr時候,等號右邊的shared_ptr會遞增其引用計數(shù),等號左邊的shared_ptr會遞減其引用計數(shù)。
實例1: Exercise 12.2:
Write your own version of the StrBlob class including the const versions of front and back.
#include <vector>
#include <string>
#include <initializer_list>
#include <memory>
#include <exception>using std::vector; using std::string;class StrBlob {
public:using size_type = vector<string>::size_type;StrBlob():data(std::make_shared<vector<string>>()) { }StrBlob(std::initializer_list<string> il):data(std::make_shared<vector<string>>(il)) {}size_type size() const { return data->size(); }bool empty() const { return data->empty(); }void push_back(const string &t) { data->push_back(t); }void pop_back() {check(0, "pop_back on empty StrBlob");data->pop_back();}std::string& front() {check(0, "front on empty StrBlob");return data->front();}std::string& back() {check(0, "back on empty StrBlob");return data->back();}const std::string& front() const {check(0, "front on empty StrBlob");return data->front();}const std::string& back() const {check(0, "back on empty StrBlob");return data->back();}private:void check(size_type i, const string &msg) const {if (i >= data->size()) throw std::out_of_range(msg);}private:std::shared_ptr<vector<string>> data;
};
test
#include "ex12_02.h"
#include <iostream>int main()
{const StrBlob csb{ "hello", "world", "pezy" };StrBlob sb{ "hello", "world", "Mooophy" };std::cout << csb.front() << " " << csb.back() << std::endl;sb.back() = "pezy";std::cout << sb.front() << " " << sb.back() << std::endl;
}
Exercise 12.6:
-
Write a function that returns a dynamically allocated vector of ints.
-
Pass that vector to another function that reads the standard input to give values to the elements.
-
Pass the vector to another function to print the values that were read. Remember to delete the vector at the appropriate time.
#include <iostream> #include <vector>
using namespace std;auto make_dynamically()
{return new vector<int>{};
}auto populate(vector<int>* vec)
{for (int i = 0; i < 4;i++)vec->push_back(i);return vec;
}auto print(vector<int>* vec) -> std::ostream&
{for (auto i : *vec) std::cout << i << " ";return std::cout;
}int main()
{auto vec = populate(make_dynamically());print(vec) << std::endl;delete vec;getchar();return 0;
}
Exercise 12.7: Redo the previous exercise, this time using shared_ptr.
#include <iostream> #include <vector> #include <memory>1
using namespace std;auto make_with_shared_ptr()
{return make_shared<vector<int>>();
}auto populate(shared_ptr<vector<int>> vec)
{for (int i = 0; i < 4; ++i)vec->push_back(i);return vec;
}auto print(shared_ptr<std::vector<int>> vec) -> std::ostream&
{for (auto i : *vec) std::cout << i << " ";return std::cout;
}int main()
{auto vec = populate(make_with_shared_ptr());print(vec) << std::endl;getchar();return 0;
}
總結
以上是生活随笔為你收集整理的【Smart_Point】动态内存与智能指针的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频50p50m和50p25m的区别?
- 下一篇: 樱桃树苗多少钱一棵