日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

【Smart_Point】动态内存与智能指针

發(fā)布時(shí)間:2023/11/27 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Smart_Point】动态内存与智能指针 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

動(dòng)態(tài)內(nèi)存

動(dòng)態(tài)內(nèi)存使用的三種原因

  • 程序不知道自己需要多少對(duì)象
  • 程序不知道所需對(duì)象的準(zhǔn)確類型
  • 程序需要在多個(gè)對(duì)線之間共享數(shù)據(jù)

    文章目錄

        • 動(dòng)態(tài)內(nèi)存
          • 動(dòng)態(tài)內(nèi)存使用的三種原因
          • 實(shí)例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個(gè)構(gòu)造函數(shù)都是用初始化成員列表來初始化data成員,令data指向一個(gè)動(dòng)態(tài)分配的vector. 默認(rèn)構(gòu)造分配一個(gè)空的vector,然后接受一個(gè)initializer_list的構(gòu)造函數(shù)通過拷貝列表中的值來初始化vector元素。

如下實(shí)例1:所示 check私有工具函數(shù),做索引檢查,處理異常參數(shù)。

StrBlob 只有一個(gè)數(shù)據(jù)成員,當(dāng)StrBlob 發(fā)生拷貝,賦值或者銷毀StrBlob 對(duì)象時(shí)候,它的shared_ptr成員也會(huì)拷貝,賦值或者銷毀

拷貝一個(gè)shared_ptr會(huì)遞增其引用計(jì)數(shù),當(dāng)將一個(gè)shared_ptr賦值給另外一個(gè)shared_ptr時(shí)候,等號(hào)右邊的shared_ptr會(huì)遞增其引用計(jì)數(shù),等號(hào)左邊的shared_ptr會(huì)遞減其引用計(jì)數(shù)

實(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;
}

總結(jié)

以上是生活随笔為你收集整理的【Smart_Point】动态内存与智能指针的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。