C++ 使用带向量的唯一_ptr

C++ 使用带向量的唯一_ptr,c++,c++11,smart-pointers,C++,C++11,Smart Pointers,我试图将shared\u ptr使用的书面代码转换为unique\u ptr,因为在代码中,它的用法似乎shared\u ptr是不必要的,这将是一个智能指针的练习。就我使用调试器所能检测到的情况而言,问题出现在v.at(I)->push_back(t)。因此,一旦输入值,程序就会崩溃 使用共享的工作代码\u ptr: #include <iostream> #include <iomanip> #include <memory> // For smart p

我试图将
shared\u ptr
使用的书面代码转换为
unique\u ptr
,因为在代码中,它的用法似乎
shared\u ptr
是不必要的,这将是一个智能指针的练习。就我使用调试器所能检测到的情况而言,问题出现在
v.at(I)->push_back(t)。因此,一旦输入值,程序就会崩溃

使用
共享的工作代码\u 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;
    }
}
#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
具有
唯一性的转换代码\u 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;
    }
}
#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
#包括
#包括
#包括
#包括
使用std::vector;
int main()
{//函数范围从这里开始
//一个向量(外部)与一个类型为double的向量(内部)保持唯一的ptr
向量v;
第{1}天的大小;
字符应答{};
双t{};
while(true)
{
大小{};
auto pDay=std::unique_ptr();
v、 推回(std::move(pDay));
标准::cout=1000.0)断裂;
v、 at(i)->推回(t);
++一,;
}
//std::cout at(0)答案;
如果(答案)='N')中断;
}
双倍总{};
大小\u t计数{};
日=1;

std::cout你的
std::unique\u ptr
没有指向任何东西。您需要使用指向向量的指针对其进行初始化。

请提供一个指针,不要包含文本图像!当您尝试减少问题空间时,问题对您来说应该变得更加明显。这不是最小的问题。独特的ptr是一种反模式。(与嵌套向量相同)@DieterLücking-为什么嵌套向量是反模式?(一般来说,我想你说的不是w.r.t.这个问题。)@davidbak,反模式是为
std::unique\u ptr
设置一个
std::vector
,因为向量可以移动(与移动
unique\u ptr
的效率相同),并且可以动态调整大小(需要时无需手动分配)。嵌套向量可以通过
std::vector
实现,它更简单、更易于使用,与
std::vector
相比没有任何实际缺点@snr:uising
std::make_shared
创建
std::shared\u ptr
。分配控制记录和对象的。如果您只是默认构造了一个共享指针,那么它也会失败。@snr请参阅std::make_unique(相当于std::make_shared)@snr:您说它是“工作代码”。你在撒谎吗?解决方案与c++14兼容@DieterLücking如何在c++11中初始化它@迪特马克吕尔