gpt4 book ai didi

c++ - 如何在yaml cpp中保留插入顺序

转载 作者:太空狗 更新时间:2023-10-29 23:16:30 26 4
gpt4 key购买 nike

我创建了一个包含 keysvalues 作为字符串的 yamlmap,我已将其保存到文本文件中。加载相同的文本文件时,包含字符串的键由 yaml 自动排序。现在我想按照我保存的顺序加载字符串名称。为了解决这个问题,我必须在函数 saveSettings() 和 loadSettings 中做些什么。谢谢

std::vector<SettingsPair*> settingsvector;std::list<std::string> settings_list;
class SettingsPair
{
public:
/**
* @brief standard constructor
*/
SettingsPair();
SettingsPair(QLineEdit *le, QLabel *la);
~SettingsPair();
QLineEdit* line_edit;
QLabel* label;

};


void task1::saveSettings()
{
QString filename = QFileDialog::getSaveFileName(this,
tr("save YAML File "),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
std::ofstream ofstr;
std::string file= filename.toStdString();
YAML::Emitter out;
ofstr.open(file.c_str());
out << YAML::BeginMap;
for(std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
list_iter != settingsvector.end(); list_iter++)
{
QString key_lineedit = (*list_iter)->line_edit->text();
QString key_label = (*list_iter)->label->text();
out << YAML::Key << key_label.toStdString();
out << YAML::Value <<key_lineedit.toStdString();

}
ofstr<<out.c_str();
out << YAML::EndMap;

std::cout<<"here is the output while saving the file:\n"<<out.c_str() ;
ofstr.close();

}

void task1::loadSettings()
{
QString filename = QFileDialog::getOpenFileName(this,
tr("Open File"),"C:/Users/marthala/Desktop/ws/settingstask","ALL files(*.*);;YAML File(*.yaml)");
//getOpenFileName will retuns the file path of the file selected

if(filename.isEmpty())
{
return;
}
else
{
try
{

std::ifstream fin(filename.toStdString().c_str());
YAML::Parser parser(fin);
YAML::Node doc;
while(parser.GetNextDocument(doc))
{
std::vector<SettingsPair*>::iterator list_iter = settingsvector.begin();
if(list_iter != settingsvector.end())
{
std::string key_, value_;
for(YAML::Iterator it=doc.begin();it!=doc.end();++it)
{

it.first() >> key_;
it.second() >> value_;

QString str1 = QString::fromUtf8(key_.c_str());
QString str2 = QString::fromUtf8(value_.c_str());
(*list_iter)->label->setText(str1);
(*list_iter)->line_edit->setText(str2);
list_iter++;

std::cout<<"here is the output while loading the file:\n"<< key_<<":\n"<<value_;
}
}
}

}
catch(YAML::ParserException &e)
{
std::cout<<"YAML Exception caught"<<e.what()<<std::endl;
}
}
}

最佳答案

目前正在考虑此功能。请参阅issue report在项目页面上获取更多详细信息。

关于c++ - 如何在yaml cpp中保留插入顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23540336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com