好的,我有以下矢量:
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
的值是正确的,是writer
和this->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解决了这个问题。