C ++ ostream_iterator创建抛出异常

时间:2016-07-18 16:29:14

标签: c++ visual-studio-2013

好的,我有以下矢量:

std::vector< std::pair<std::shared_ptr<std::fstream>, uint32_t> > writer = {
    { std::make_shared<std::fstream>("file1.csv", std::fstream::out | std::fstream::trunc), bin_c1 },
    { std::make_shared<std::fstream>("file2.csv", std::fstream::out | std::fstream::trunc), bin_c1 },
    { std::make_shared<std::fstream>("file3.csv", std::fstream::out | std::fstream::trunc), bin_c2 },
    { std::make_shared<std::fstream>("file4.csv", std::fstream::out | std::fstream::trunc), bin_c2 },
    { std::make_shared<std::fstream>("file5.csv", std::fstream::out | std::fstream::trunc), bin_c3 },
    { std::make_shared<std::fstream>("file6.csv", std::fstream::out | std::fstream::trunc), bin_c3 }
};

然后我有以下代码:

for_each_idx(this->data.begin(), this->data.end(), 0, [&writer](unsigned index, std::vector<float> value) {
     auto it_start  = value.begin();
     auto it_end    = value.begin() + std::min(value.size(), writer.at(index).second);
     auto it_stream = std::ostream_iterator<float>(*writer.at(index).first << std::fixed, ",");

     std::transform(it_start, it_end, it_stream, [](float num){ /* Do stuff */ });
});

问题出在这一行:

auto it_stream = std::ostream_iterator<float>(*writer.at(index).first << std::fixed, ",");

这给了我一个access violation错误。起初我认为这是.at(index),但index的值是正确的,是writerthis->data向量具有相同的大小。那么,我错过了什么?

编辑: 好的,我找到了一种方法让它发挥作用。我改变了作家的创作:

std::vector< std::pair<std::shared_ptr<std::fstream>, uint32_t> > writer;

writer.push_back({ std::make_shared<std::fstream>("file1.csv", std::fstream::out | std::fstream::trunc), bin_c1 });
writer.push_back({ std::make_shared<std::fstream>("file2.csv", std::fstream::out | std::fstream::trunc), bin_c1 });
writer.push_back({ std::make_shared<std::fstream>("file3.csv", std::fstream::out | std::fstream::trunc), bin_c2 });
writer.push_back({ std::make_shared<std::fstream>("file4.csv", std::fstream::out | std::fstream::trunc), bin_c2 });
writer.push_back({ std::make_shared<std::fstream>("file5.csv", std::fstream::out | std::fstream::trunc), bin_c3 });
writer.push_back({ std::make_shared<std::fstream>("file6.csv", std::fstream::out | std::fstream::trunc), bin_c3 });

但是,有人可以解释为什么会这样做吗?当我使用初始化列表创建writer时,它实际上将每个shared_ptr视为未初始化的指针?

编辑: 安装Visual Studio 2013 Update 5解决了这个问题。

0 个答案:

没有答案