将unique_ptr与向量一起使用

时间:2016-10-01 17:23:43

标签: c++ c++11 smart-pointers

我尝试将shared_ptr使用的书面代码转换为unique_ptr,因为在其使用代码中似乎不需要shared_ptr,这将是一个智能指针的练习。据我所知,调试器问题出在v.at(i)->push_back(t);上。因此,只要输入一个值,程序就会崩溃。

使用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

1 个答案:

答案 0 :(得分:1)

您的std::unique_ptr<std::vector<double>>没有指出任何事情。您需要使用指向矢量的指针对其进行初始化。