我是 RapidXML 的新手。我想构建一个 Xml 文档并将其打印到一个文件中。一切正常,但我不确定我是否正确地完成了流程的一部分:
将属性添加到 double 节点。
我正在使用 std c++ 的东西:
double value = 1.0;
std::ostringstream strs;
strs << value ;
std::string str = strs.str();
char* numBuff = doc.allocate_string(str.c_str());
xml_attribute<> *attr = doc.allocate_attribute("name",numBuff);
nodeRef->append_attribute(attr);
有没有更优雅/更快的方法?像(一厢情愿的想法):
double value = 1.0;
char* numBuff = doc.allocate_string_from_value(value);
xml_attribute<> *attr = doc.allocate_attribute("name",numBuff);
我需要将大量 double 值保存到我的 xml 文件中,因此性能是我的主要关注点。
您好, 奥利弗
最佳答案
我知道这是一个老话题,但还没有确定的答案。 要使用更高效的函数转换值,更喜欢旧的 C 语法 (printf),而不是 C++ 流式处理,后者效率更高。我是这样实现的:
声明转换函数...
char* double2char(double value) {
char tmpval[64];
sprintf(tmpval,"%f",value);
return doc->allocate_string(tmpval);
}
...在代码中使用...
double value = 1.0;
xml_attribute<> *attr = doc.allocate_attribute("name",double2char(value));
...这是我的实现,也许不是最好的,但更优雅和更快...
最好的问候。
附言。对不起我的巴西英语。
关于c++ - 将数字( double / float )作为属性添加到 RapidXML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4583409/