Okay, I have the following vector:
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 }
};
and then I have the following code:
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 */ });
});
The problem is at this line:
auto it_stream = std::ostream_iterator<float>(*writer.at(index).first << std::fixed, ",");
which gives me an access violation
error. At first I thought it was the .at(index)
but index
's value is correct and yes writer
and this->data
vectors have the same size. So, what am I missing?
EDIT: Okay I found a way to make it work. I changed the writer creation:
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 });
Still, can someone explain why this works and when I create the writer
using the initializer list it actually treats every shared_ptr
as uninitialized pointer?
EDIT: Installing Visual Studio 2013 Update 5 solved the problem.