c++ store vector<vector<float>> in file and reverse
2017-11-08 13:49
429 查看
方案来源:
https://stackoverflow.com/questions/15707537/how-to-save-vectorvectorvectorfloat-in-a-file-and-retrieve-data
解决方法:
同时再编译过程中加入相应的boost库
ex:
方法2:
来源:https://stackoverflow.com/questions/43230542/write-vectorvectorfloat-to-binary-file
以上两种方法,具体消耗时间未测试。
序列化资料:
https://www.ibm.com/developerworks/cn/aix/library/au-boostserialization/
http://blog.csdn.net/lanxuezaipiao/article/details/24845625
https://stackoverflow.com/questions/15707537/how-to-save-vectorvectorvectorfloat-in-a-file-and-retrieve-data
解决方法:
#include <boost/archive/binary_iarchive.hpp> #include <boost/archive/binary_oarchive.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/export.hpp> #include <boost/serialization/vector.hpp> #include <boost/serialization/list.hpp> void saveFeaturesFile(vector<vector<float>> &features, string filename){ ofstream out(filename.c_str()); stringstream ss; boost::archive::binary_oarchive oa(ss); oa << features; out << ss.str(); out.close(); } void loadFeaturesFile(vector<vector<float>> &features, string filename){ ifstream in(filename.c_str()); boost::archive::binary_iarchive ia(in); ia >> features; in.close(); }
同时再编译过程中加入相应的boost库
ex:
g++ -std=c++11 -o test Test.cpp **-L/usr/local/lib -lboost_iostreams -lboost_serialization**
方法2:
void saveVector(std::string path, const vector<vector<float> >& myVector) { std::ofstream FILE(path, std::ios::out | std::ofstream::binary); // Store size of the outer vector int s1 = myVector.size(); FILE.write(reinterpret_cast<const char *>(&s1), sizeof(s1)); // Now write each vector one by one for (auto& v : myVector) { // Store its size int size = v.size(); FILE.write(reinterpret_cast<const char *>(&size), sizeof(size)); // Store its contents FILE.write(reinterpret_cast<const char *>(&v[0]), v.size()*sizeof(float)); } FILE.close(); } void readVector(std::string path, vector<vector<float> >& myVector) { ifstream FILE(path, std::ios::in | std::ifstream::binary); int size = 0; FILE.read(reinterpret_cast<char *>(&size), sizeof(size)); myVector.resize(size); for (int n = 0; n < size; ++n) { int size2 = 0; FILE.read(reinterpret_cast<char *>(&size2), sizeof(size2)); float f; for ( int k = 0; k < size2; ++k ) { FILE.read(reinterpret_cast<char *>(&f), sizeof(f)); myVector .push_back(f); } } }
来源:https://stackoverflow.com/questions/43230542/write-vectorvectorfloat-to-binary-file
以上两种方法,具体消耗时间未测试。
序列化资料:
https://www.ibm.com/developerworks/cn/aix/library/au-boostserialization/
http://blog.csdn.net/lanxuezaipiao/article/details/24845625
相关文章推荐
- C++中将vector<float>的数据进行base64加密存储到数据库中(内含base64源码)
- Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- c++<vector>AND<array>
- 《C++ Concurrency in Action》笔记4 vector<thread>
- C++ 错误: ‘std::vector<float> feat’的声明隐藏了一个形参
- C++vector<vector<double>> and opencv Mat
- C++ 中对vector<T*> 数组的查找和排序
- 运行Spark项目出错error: error while loading <root>, error in opening zip file
- C++ <vector>
- C++文本查询程序 定义类管理数据 用智能指针 用StrBlob代替vector<string> C++Primer 练习12.32
- C/C++中容器vector使用方法<第一弹>
- 继 <ZMQ的学习和研究> 学习代码 Request-reply client in C++
- [置顶] 从零开始学C++之对象语义与值语义、资源管理(RAII、资源所有权)、模拟实现auto_ptr<class>、实现Ptr_vector .
- New <%: %> Syntax for HTML Encoding Output in ASP.NET 4 (and ASP.NET MVC 2)
- SSIS <none> text qualifier results in text file containing "_x003C_none_x003E"
- difference between <? super T> and <? extends T> in Java
- c++ 中 const_iterator 和 const vector<>::iterator的区别
- <设计模式>系列汇总 By python and C++
- c++ 模板 vector<T>::const_iterator报错