gpt4 book ai didi

c++ - 使用结构的二维 vector

转载 作者:行者123 更新时间:2023-11-30 04:55:12 26 4
gpt4 key购买 nike

所以我有以下代码,我在其中读取 XML 文件并将值保存到 vector 中。我对 vector 没有太多经验,但现在我有了参数“motor”,我希望能够获取电机值,并从该 vector 中读取参数。

我必须向我的结构添加一些东西吗?或者初始化一个二维 vector vector<vector <NameValue>> motors;如果我确实初始化了一个 2D vector ,我将如何填充该 vector 。或者有什么好的指南可以帮助我完全理解 vector ?

这是 TinyXML 的文档。 http://www.grinninglizard.com/tinyxmldocs/index.html

#include <QCoreApplication>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <tinyxml.h>

using namespace std;

struct NameValue
{
string motor;
string name;
string value;
};

vector<NameValue> read()
{ //READING XML FILE
QString directory;
string file;
directory = QCoreApplication::applicationDirPath();
file = directory.toUtf8().constData();
file = file + "/Profile.xml";
cout<<file<<endl;
TiXmlDocument doc (file);
if(!doc.LoadFile()){
cout<<"Cannot open file"<<endl;}

vector<NameValue> xmlread;
NameValue elem;

TiXmlElement *pRoot, *pParm, *pMotor;
pRoot = doc.FirstChildElement("PRO");
if (pRoot) //parsing
{
pMotor = pRoot->FirstChildElement("Motor");
if(pMotor){
while (pMotor)
{
elem.motor = pMotor->Attribute("value");
pParm = pMotor->FirstChildElement("PARAMETER");
while (pParm)
{
elem.name = pParm->Attribute("name");
elem.value = pParm->Attribute("value");
xmlread.push_back(elem);
pParm = pParm->NextSiblingElement("PARAMETER");
}
pMotor = pMotor->NextSiblingElement("Motor");

}
}
}
return xmlread;
}
int main (int argc, char *argv[])
{
vector<NameValue> elems = read();
cout<<elems[0].motor<<endl;

}

这是我的 XML 文件的片段:

<PRO>
<Motor value = "1">
<PARAMETER name = "SPEED" value = "100" />
<PARAMETER name = "LOOP" value = "1" />
<PARAMETER name = "DEADBAND" value = "10" />
</Motor>
<Motor value = "2">
<PARAMETER name = "SPEED" value = "100" />
<PARAMETER name = "LOOP" value = "1" />
<PARAMETER name = "DEADBAND" value = "10" />
</Motor>
<Motor value = "3">
<PARAMETER name = "SPEED" value = "100" />
<PARAMETER name = "LOOP" value = "1" />
<PARAMETER name = "DEADBAND" value = "10" />
</Motor>
</PRO>

最佳答案

为了生成 2D vector ,您已经拥有的东西需要很少的修改。只需使用 emplace_back就地构建内部 vector :

vector<vector<NameValue>> xmlread2D;
NameValue elem;

TiXmlElement *pRoot, *pParm, *pMotor;
pRoot = doc.FirstChildElement("PRO");
if (pRoot)
{
pMotor = pRoot->FirstChildElement("Motor");
while (pMotor)
{
// allocate new inner vector
xmlread2D.emplace_back();
auto& xmlread = xmlread2D.back();
// in C++17 the above can be replaced with a single line

// unmodified inner loop
elem.motor = pMotor->Attribute("value");
pParm = pMotor->FirstChildElement("PARAMETER");
while (pParm)
{
elem.name = pParm->Attribute("name");
elem.value = pParm->Attribute("value");
xmlread.push_back(elem);
pParm = pParm->NextSiblingElement("PARAMETER");
}
pMotor = pMotor->NextSiblingElement("Motor");
}
}

关于c++ - 使用结构的二维 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53196263/

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