C++實現(xiàn)數(shù)據(jù)文件存儲與加載
更新時間:2019年06月17日 10:49:27 作者:你是天使放縱我的固執(zhí)
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)數(shù)據(jù)文件存儲與加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C++實現(xiàn)數(shù)據(jù)文件存儲與加載的具體代碼,供大家參考,具體內(nèi)容如下
首先請先確認(rèn)已經(jīng)安裝好了opencv3及以上版本。
#include <opencv2/opencv.hpp> #include <iostream> #include <string> using namespace cv; using namespace std;
存儲
then
int main()
{
//創(chuàng)造一些要存的數(shù)據(jù)先
string words = "hello, my guys!";
float n = 3.1415926;
Mat m = Mat::eye(3, 3, CV_32F);
//開始創(chuàng)建存儲器
FileStorage save("data.yml", FileStorage::WRITE);// 你也可以使用xml格式
save << "words" << words;
save << "number" << n;
save << "matrix" << m;
save.release();
//存儲完畢
cout << "finish storing" << endl;
加載
//加載數(shù)據(jù),類似Python字典的用法,創(chuàng)建加載器
FileStorage load("data.yml", FileStorage::READ);
float nn;
Mat mm;
string ww;
load["words"] >> ww;
load["number"] >> nn;
load["matrix"] >> mm;
cout<< ww << endl << nn << endl << mm;
cout << endl << "That's the end";
load.release();
return 0;
}
完整代碼
#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
using namespace cv;
using namespace std;
int main()
{
string words = "hello, my guys!";
float n = 3.1415926;
Mat m = Mat::eye(3, 3, CV_32F);
FileStorage save("data.yml", FileStorage::WRITE);
save << "words" << words;
save << "number" << n;
save << "matrix" << m;
save.release();
cout << "finish storing" << endl;
FileStorage load("data.yml", FileStorage::READ);
float nn;
Mat mm;
string ww;
load["words"] >> ww;
load["number"] >> nn;
load["matrix"] >> mm;
cout<< ww << endl << nn << endl << mm;
cout << endl << "That's the end";
load.release();
return 0;
}
演示結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言結(jié)構(gòu)體數(shù)組同時賦值的另類用法
今天小編就為大家分享一篇關(guān)于C語言結(jié)構(gòu)體數(shù)組同時賦值的另類用法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
C++賦值函數(shù)+移動賦值函數(shù)+移動構(gòu)造函數(shù)詳解
這篇文章主要介紹了C++賦值函數(shù)+移動賦值函數(shù)+移動構(gòu)造函數(shù)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08

