2016-10-01 108 views
-1

我試圖將shared_ptr使用的書面代碼轉換爲unique_ptr,因爲在它的使用代碼中似乎不必要,它將是一個智能指針的練習。據我可以用調試器檢測到的問題是v.at(i)->push_back(t);。所以,一旦輸入值,程序就會崩潰。使用unique_ptr與矢量

shared_ptr與工作碼:

#include <iostream> 
#include <iomanip> 
#include <memory> // For smart pointers 
#include <vector> // For vector container 
#include <locale> // For toupper() 
using std::vector; 
using std::shared_ptr; 
int main() 
{ 
    vector <shared_ptr<vector<double>>>records; // Temperature records by days 
    size_t day{ 1 }; // Day number 
    char answer{}; // Response to prompt 
    double t{}; // A temperature 
    while (true) // Collect temperatures by day 
    { // Vector to store current day's temperatures created on the heap 
     auto pDay = std::make_shared<vector<double>>(); 
     records.push_back(pDay); // Save pointer in records vector 
     std::cout << "Enter the temperatures for day " << day++ 
      << " separated by spaces. Enter 1000 to end:\n"; 
     while (true) 
     { // Get temperatures for current day 
      std::cin >> t; 
      if (t == 1000.0) break; 
      pDay->push_back(t); 
     } 
     std::cout << "Enter another day's temperatures (Y or N)? "; 
     std::cin >> answer; 
     if (toupper(answer) == 'N') break; 
    } 
    double total{}; 
    size_t count{}; 
    day = 1; 
    std::cout << std::fixed << std::setprecision(2) << std::endl; 
    for (auto record : records) 
    { 
     std::cout << "\nTemperatures for day " << day++ << ":\n"; 
     for (auto temp : *record) 
     { 
      total += temp; 
      std::cout << std::setw(6) << temp; 
      if (++count % 5 == 0) std::cout << std::endl; 
     } 
     std::cout << "\nAverage temperature: " << total/count << std::endl; 
     total = 0.0; 
     count = 0; 
    } 
} 

輸出:

23 34 29 36 1000 
Enter another day's temperatures (Y or N)? y 
Enter the temperatures for day 2 separated by spaces. Enter 1000 to end: 
34 35 45 43 44 40 37 35 1000 
Enter another day's temperatures (Y or N)? y 
Enter the temperatures for day 3 separated by spaces. Enter 1000 to end: 
44 56 57 45 44 32 28 1000 
Enter another day's temperatures (Y or N)? n 

Temperatures for day 1: 
23.00 34.00 29.00 36.00 
Average temperature: 30.50 

Temperatures for day 2: 
34.00 35.00 45.00 43.00 44.00 
40.00 37.00 35.00 
Average temperature: 39.13 

Temperatures for day 3: 
44.00 56.00 57.00 45.00 44.00 
32.00 28.00 
Average temperature: 43.71 

轉換後的代碼與unique_ptr

#include <iostream> 
#include <vector> 
#include <memory> 
#include <iomanip> 

using std::vector; 


int main() 
{ // Function scope starts here 

    // a vector(outside) holding unique_ptrs to a vector(inside) which type is double 
    vector<std::unique_ptr<vector<double>>> v; 
    size_t day{ 1 }; 
    char answer{}; 
    double t{}; 


    while (true) 
    { 
     size_t i{}; 
     auto pDay = std::unique_ptr<vector<double>>(); 
     v.push_back(std::move(pDay)); 

     std::cout << "Enter the temperatures for day " << day++ 
      << " separated by spaces. Enter 1000 to end:\n"; 

     while (true) 
     { 
      std::cin >> t; 
      if (t >= 1000.0) break; 
      v.at(i)->push_back(t); 
      ++i; 
     } 

     //std::cout << v.at(0)->at(0) << std::endl; 
     std::cout << "Enter another day's temperatures (Y or N)? "; 
     std::cin >> answer; 
     if (toupper(answer) == 'N') break; 
    } 

    double total{}; 
    size_t count{}; 
    day = 1; 
    std::cout << std::fixed << std::setprecision(2) << std::endl; 

    for (auto const& record : v) 
    { 
     std::cout << "\nTemperatures for day " << day++ << ":\n"; 
     for (auto temp : *record) 
     { 
      total += temp; 
      std::cout << std::setw(6) << temp; 
      if (++count % 5 == 0) std::cout << std::endl; 
     } 
     std::cout << "\nAverage temperature: " << total/count << std::endl; 
     total = 0.0; 
     count = 0; 
    } 

} // Function scope ends here 

enter image description here

+4

請提供[mcve],不要包含文字圖片!當你嘗試和減少問題空間時,這個問題應該變得更加明顯。 – Barry

+0

這不是最小的。 –

+3

unique_ptr >是一種反模式。 (相同的嵌套向量) –

回答

1

你的std::unique_ptr<std::vector<double>>不指向任何東西。您需要使用指向矢量的指針來初始化它。

+0

帶'shared_ptr()'的代碼怎麼樣?它指的是什麼? – snr

+0

@snr:你創建'std :: shared_ptr >'使用'std :: make_shared >'。分配控制記錄和一個對象。如果你只是默認構建了一個共享指針,它也會失敗。 –

+0

@snr參見std :: make_unique(相當於std :: make_shared) –