C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載
本文實(shí)例為大家分享了C++實(shí)現(xiàn)數(shù)據(jù)文件存儲(chǔ)與加載的具體代碼,供大家參考,具體內(nèi)容如下
首先請(qǐng)先確認(rèn)已經(jīng)安裝好了opencv3及以上版本。
#include <opencv2/opencv.hpp> #include <iostream> #include <string> using namespace cv; using namespace std;
存儲(chǔ)
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)建存儲(chǔ)器
FileStorage save("data.yml", FileStorage::WRITE);// 你也可以使用xml格式
save << "words" << words;
save << "number" << n;
save << "matrix" << m;
save.release();
//存儲(chǔ)完畢
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é)果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言求圓周率的簡(jiǎn)單實(shí)現(xiàn)方法
這篇文章主要介紹了C語(yǔ)言求圓周率的簡(jiǎn)單實(shí)現(xiàn)方法,涉及C語(yǔ)言數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05
C++實(shí)現(xiàn)教務(wù)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)教務(wù)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C++實(shí)現(xiàn)比特幣系統(tǒng)的源碼
這篇文章主要介紹了C++實(shí)現(xiàn)比特幣系統(tǒng)的源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
C語(yǔ)言結(jié)構(gòu)體數(shù)組同時(shí)賦值的另類用法
今天小編就為大家分享一篇關(guān)于C語(yǔ)言結(jié)構(gòu)體數(shù)組同時(shí)賦值的另類用法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
C語(yǔ)言冒泡排序法的實(shí)現(xiàn)(升序排序法)
這篇文章主要介紹了C語(yǔ)言冒泡排序法的實(shí)現(xiàn)(升序排序法),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解
這篇文章主要介紹了C++賦值函數(shù)+移動(dòng)賦值函數(shù)+移動(dòng)構(gòu)造函數(shù)詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08

