【发布时间】:2018-11-24 14:52:52
【问题描述】:
我有一个问题,我需要将流大小值传递给向量。如果我没有将它传递给固定大小。它将使用带有千元素的 emplace_back 开销。错误编译器可能导致数据丢失。
ifstream input(i_inputFilePath, ios::binary);
if (input.is_open())
{
ofstream output(o_outputFilePath, ios::binary);
std::array<char, 1024> buffer;
while (true) {
input.read(buffer.data(), buffer.size());
streamsize dataSize = input.gcount();
if (dataSize)
{
std::vector<char> data(dataSize); // here the problem
for (DWORD i = 0; i < dataSize; i++)
{
data.emplace_back(buffer[i]);
}
std::rotate(data.begin(), data.begin() + 1, data.end());
output.write(data.data(), dataSize);
}
else
{
output.close();
input.close();
break;
}
}
}
else
{
cout << "File is not exist";
}
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages、the SO tour、阅读how to ask good questions,以及this question checklist。还学习如何创建minimal reproducible example。如果询问构建错误,请包含复制粘贴的完整和完整输出,并在代码中添加注释以显示错误发生的位置。
-
@Someprogrammerdude 已经满了。我用评论编辑了我的问题以指出问题。
标签: c++ windows visual-studio