C++ 保存向量<;int>;归档
真奇怪,我在stackoverflow上没有找到这个问题的答案 我想将C++ 保存向量<;int>;归档,c++,c++11,serialization,save,C++,C++11,Serialization,Save,真奇怪,我在stackoverflow上没有找到这个问题的答案 我想将std::vector保存到文件中。 在不同的地方,我发现了以下代码: std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512}; std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary); std::copy(v.begin(), v.end(), std::ostreambuf_
std::vector
保存到文件中。
在不同的地方,我发现了以下代码:
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostreambuf_iterator<char>(outfile));
outfile.close();
我的想法是将
std::ostreambuf_迭代器
更改为std::ostreambuf_迭代器
,但这不起作用。编译器抛出错误:
error: no matching function for call to ‘std::ostreambuf_iterator<int>::ostreambuf_iterator(std::ofstream&)’
std::copy(v.begin(), v.end(), std::ostreambuf_iterator<int>(outfile));
错误:调用'std::ostreambuf_迭代器::ostreambuf_迭代器(std::ofstream&')时没有匹配的函数
std::copy(v.begin()、v.end()、std::ostreambuf_迭代器(outfile));
我怎样才能避开这个问题呢?您正在寻找的是
std::ostream\u迭代器
,而不是std::ostreambuf\u迭代器
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
这只是将
int
向量中的值写入输出流,就像使用您要查找的是std::ostream\u迭代器
,而不是std::ostreambuf\u迭代器
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
这只是将int
向量中的值写入输出流,就像使用一样,在创建流时可以选择性地删除std::ofstream::binary
(也可以删除std::ios::out
,因为这是ofstream
的默认值)
主要是使用std::ostream_迭代器
而不是std::ostreambuf_迭代器
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
向量v{0,1,2,4,8,16,32,64128256512};
标准:流出管(“试验数据”);
std::copy(v.begin()、v.end()、std::ostream_迭代器(outfile));
outfile.close();
您可能还希望在创建ostream\u迭代器时添加分隔符
或者,在创建流时删除std::ofstream::binary
(您也可以删除std::ios::out
,因为这是ofstream
的默认值)
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
主要是使用std::ostream_迭代器
而不是std::ostreambuf_迭代器
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
向量v{0,1,2,4,8,16,32,64128256512};
标准:流出管(“试验数据”);
std::copy(v.begin()、v.end()、std::ostream_迭代器(outfile));
outfile.close();
在创建
ostream\u迭代器
时,您可能还需要添加一个分隔符。如果希望将整数作为字符写入文件,则可以使用:
std::vector<int> v{0,1,2,4,8,16,32,64,128,256,512};
std::ofstream outfile("test.data", std::ios::out | std::ofstream::binary);
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(outfile));
outfile.close();
std::for_each(v.begin()、v.end()、[&outfile](int x){outfile如果希望将整数作为字符写入文件,则可以使用:
std::for_each(v.begin()、v.end()、[&outfile](int x){outfile您可以序列化为二进制,只要记住endianness之类的问题,您就可以了。基本上,使用std::of stream和ifstream write()和read()
下面显示了两个序列化和非序列化程序,即quick hacks,因此可能会出现错误:
#include <fstream>
#include <stdexcept>
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
using std::ofstream;
using std::ios;
int main()
{
vector<int> datavec{0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
ofstream outfile;
outfile.open ("datafile.bin", ios::out | ios::trunc | ios::binary);
for (auto val : datavec) {
outfile.write(reinterpret_cast<const char *>(&val), sizeof(int));
if (outfile.bad()) {
throw std::runtime_error("Failed to write to outfile!");
}
}
cout << "Wrote data to file. Done.\n";
}
//////////////////////////////////////////////////
#include <fstream>
#include <stdexcept>
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
using std::ifstream;
using std::ios;
using std::ios_base;
int main()
{
vector<int> datavec;
ifstream infile;
infile.open ("datafile.bin", ios::in | ios::binary);
while (infile) {
int val;
infile.read(reinterpret_cast<char *>(&val), sizeof(int));
if (infile.bad()) {
throw std::runtime_error("Failed to read from infile!");
}
if (infile.eof()) break;
datavec.push_back(val);
}
cout << "Read data file. Contents of datavec: \n";
for (auto val : datavec) {
cout << val << ", ";
}
cout << "\nDone\n";
}
#包括
#包括
#包括
#包括
使用std::cout;
使用std::vector;
使用std::of流;
使用std::ios;
int main()
{
向量数据向量{0,1,2,4,8,16,32,64,128,256,512};
出流孔的直径;
outfile.open(“datafile.bin”,ios::out | ios::trunc | ios::binary);
用于(自动val:datavec){
outfile.write(reinterpret_cast(&val),sizeof(int));
if(outfile.bad()){
抛出std::runtime_错误(“未能写入输出文件!”);
}
}
cout您可以序列化为二进制,只要记住诸如endianness之类的问题,您就可以了。基本上,使用std::ofstream和ifstream write()和read()
下面显示了两个序列化和非序列化程序,即quick hacks,因此可能会出现错误:
#include <fstream>
#include <stdexcept>
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
using std::ofstream;
using std::ios;
int main()
{
vector<int> datavec{0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
ofstream outfile;
outfile.open ("datafile.bin", ios::out | ios::trunc | ios::binary);
for (auto val : datavec) {
outfile.write(reinterpret_cast<const char *>(&val), sizeof(int));
if (outfile.bad()) {
throw std::runtime_error("Failed to write to outfile!");
}
}
cout << "Wrote data to file. Done.\n";
}
//////////////////////////////////////////////////
#include <fstream>
#include <stdexcept>
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
using std::ifstream;
using std::ios;
using std::ios_base;
int main()
{
vector<int> datavec;
ifstream infile;
infile.open ("datafile.bin", ios::in | ios::binary);
while (infile) {
int val;
infile.read(reinterpret_cast<char *>(&val), sizeof(int));
if (infile.bad()) {
throw std::runtime_error("Failed to read from infile!");
}
if (infile.eof()) break;
datavec.push_back(val);
}
cout << "Read data file. Contents of datavec: \n";
for (auto val : datavec) {
cout << val << ", ";
}
cout << "\nDone\n";
}
#包括
#包括
#包括
#包括
使用std::cout;
使用std::vector;
使用std::of流;
使用std::ios;
int main()
{
向量数据向量{0,1,2,4,8,16,32,64,128,256,512};
出流孔的直径;
outfile.open(“datafile.bin”,ios::out | ios::trunc | ios::binary);
用于(自动val:datavec){
outfile.write(reinterpret_cast(&val),sizeof(int));
if(outfile.bad()){
抛出std::runtime_错误(“未能写入输出文件!”);
}
}
cout#include
和#include
。也可能#include
@Arunmu都包括在内。只需删除|std::ofstream::binary
。无论如何,您都不需要二进制文件。std::ostreambuf\u迭代器
要求模板参数为字符类型。您可以尝试使用wchar
,但仍然需要大于65535的值失败。如果值仍然转换为整数,为什么不简单地使用@Alf将向量写入文件流。\include
和\include
。也可能\include
@Arunmu两者都包括在内。只需删除\std::ofstream::binary
。您无论如何都不需要二进制文件。std::ostreambuf_迭代器
要求模板参数为字符类型。您可以尝试使用wchar
,但对于大于65535的值,它仍然会失败。如果值仍然转换为整数,为什么不简单地使用@Alf将向量写入文件流。是的,这是最好的方法。即使您想这样做它是一个分隔符,如“\n”、“”、“”、“等。您可以将其作为第二个参数传递给std::ostream_iterator(),它将以该格式保存。是的,这是最好的方法。即使您要传递分隔符,如“\n”、“”、“”、“”、“等,也可以将其作为第二个参数传递给std::ostream_iterator())它将以这种格式保存。谢谢,这正是我想要的。在将读取值推送到向量之前,我们必须使分支if(infle.eof())break;
。否则最后一个读取值会被推送两次。@user38034很好,我用您建议的附加行修改了src代码。谢谢,这正是我想要的。在将读取值推送到向量之前,我们必须使分支if(infle.eof())break;
。否则最后一个读取值会被推送两次。@user38034很好,我用add修改了src代码