Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   struct containing vector? (http://www.velocityreviews.com/forums/t713564-struct-containing-vector.html)

cerr 02-01-2010 04:45 PM

struct containing vector?
 
Hi There,

I have a little issue going here:
I have a xml configuration file that looks kinda like this:
<PID>
<DESCRIPTION>Steveston Hwy</DESCRIPTION>
<BCAST>192.168.101.255</BCAST>
<DESTINATION>
<DESTNO>1531</DESTNO>
<DESTNO>301</DESTNO>
<DESTNO>12555</DESTNO>
</DESTINATION>
</PID>
Now there could be various <PID> tags as well as each <PID> would
likely have multiple <DESTNO> tags.
How do I store this best in my app?
I thoughtr I'd declare a struct like this:
struct structPID
{
string strBcast;
string strDescription;
vector<int> Dest;
};
and just extend the array when there's more PIDs in that file.
But when I read out the file i'm using my xml parser and i thought i'd
do something like this:
//i_PIDlist is the pointer to a variable decalred private in class
PIDClient.
bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
*i_PIDlist)
{
RSXMLParser xmlPIDData(Configfile,
RSXMLParser::FILE);

if (!xmlPIDData.IsDataGood()) {
OUTPUT(std::cerr << "Document " << conf->
intersectionDataFile << " is not parsed
successfully.\n";
)
return false;
}

string strPID = xmlPIDData.GetNodeData("PID"); //PID
while (strPID.size() > 0) {
RSXMLParser xmlPID(strPID); //PID details
structPID tmpPID;

tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPT ION");

string strDest = xmlPIDData.GetNodeData("DESTINATION");
while(strDest.size() > 0) {
RSXMLParser xmlPIDDest(strDest);
xmlPIDDest.GetNodeData("DESTNO");
tmpPID.Dest->push_back();
}
i_PIDlist->push_back(tmpPID);
//delete tmpPID;
//tmpPID=NULL;

strPID = xmlPIDData.GetNext();
}

return true;
}
But there's two problems i'm seeing just right out of the bat:
If the outer while loop goes twice, how would tmpPID be re-declared?
I thought about declaring it on the heap and then calling delete in
the end of the funtion before it loops around but even then,
I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
pionter that gets ereased soon after...

Thanks for a little help and guidance here.

Ron

cerr 02-01-2010 05:31 PM

Re: struct containing vector?
 
On Feb 1, 8:45*am, cerr <ron.egg...@gmail.com> wrote:
> Hi There,
>
> I have a little issue going here:
> I have a xml configuration file that looks kinda like this:
> <PID>
> * <DESCRIPTION>Steveston Hwy</DESCRIPTION>
> * <BCAST>192.168.101.255</BCAST>
> * <DESTINATION>
> * * <DESTNO>1531</DESTNO>
> * * <DESTNO>301</DESTNO>
> * * <DESTNO>12555</DESTNO>
> * </DESTINATION>
> </PID>
> Now there could be various <PID> tags as well as each <PID> would
> likely have multiple <DESTNO> tags.
> How do I store this best in my app?
> I thoughtr I'd declare a struct like this:
> struct structPID
> {
> * * * * string strBcast;
> * * * * string strDescription;
> * * * * vector<int> Dest;};
>
> and just extend the array when there's more PIDs in that file.
> But when I read out the file i'm using my xml parser and i thought i'd
> do something like this:
> //i_PIDlist is the pointer to a variable decalred private in class
> PIDClient.
> bool PIDClient::readPIDConfig(string Configfile, vector<structPID>
> *i_PIDlist)
> {
> * * RSXMLParser xmlPIDData(Configfile,
> * * * * * * * * * * * * * * *RSXMLParser::FILE);
>
> * * if (!xmlPIDData.IsDataGood()) {
> * * * * OUTPUT(std::cerr << "Document " << conf->
> * * * * * * * * * * * * *intersectionDataFile << " is not parsed
> successfully.\n";
> * * * * * * * )
> * * * * return false;
> * * }
>
> * * string strPID = xmlPIDData.GetNodeData("PID"); * * *//PID
> * * while (strPID.size() > 0) {
> * * * * RSXMLParser xmlPID(strPID); * * * * * * * * * * //PID details
> * * * * structPID tmpPID;
>
> * * * * tmpPID.strBcast=xmlPID.GetNodeData("BCAST");
> * * * * tmpPID.strDescription=xmlPID.GetNodeData("DESCRIPT ION");
>
> * * * * string strDest = xmlPIDData.GetNodeData("DESTINATION");
> * * * * while(strDest.size() > 0) {
> * * * * * RSXMLParser xmlPIDDest(strDest);
> * * * * * xmlPIDDest.GetNodeData("DESTNO");
> * * * * * tmpPID.Dest->push_back();
> * * * * }
> * * * * i_PIDlist->push_back(tmpPID);
> * * * * //delete tmpPID;
> * * * * //tmpPID=NULL;
>
> * * * * strPID = xmlPIDData.GetNext();
> * * }
>
> * * return true;}
>
> But there's two problems i'm seeing just right out of the bat:
> If the outer while loop goes twice, how would tmpPID be re-declared?
> I thought about declaring it on the heap and then calling delete in
> the end of the funtion before it loops around but even then,
> I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
> pionter that gets ereased soon after...
>
> Thanks for a little help and guidance here.
>
> Ron


Ah,

I think I've figured out how I can do it:
How about this?

while (strPID.size() > 0) {
RSXMLParser xmlPID(strPID); //PID details
structPID *tmpPID = new structPID;

tmpPID->strBcast=xmlPID.GetNodeData("BCAST");
tmpPID->strDescription=xmlPID.GetNodeData("DESCRIPTION" );

string strDest = xmlPIDData.GetNodeData("DESTINATION");
while(strDest.size() > 0) {
RSXMLParser xmlPIDDest(strDest);
tmpPID->Dest.push_back(atoi(xmlPIDDest.GetNodeData("DESTN O").c_str
()));
}
i_PIDlist->push_back((*tmpPID));
delete tmpPID;
tmpPID=NULL;

strPID = xmlPIDData.GetNext();
}

Bart van Ingen Schenau 02-03-2010 11:57 AM

Re: struct containing vector?
 
On Feb 1, 5:45*pm, cerr <ron.egg...@gmail.com> wrote:
> Hi There,
>
> I have a little issue going here:
> I have a xml configuration file that looks kinda like this:

<snip>
> * * string strPID = xmlPIDData.GetNodeData("PID"); * * *//PID
> * * while (strPID.size() > 0) {
> * * * * RSXMLParser xmlPID(strPID); * * * * * * * * * * //PID details
> * * * * structPID tmpPID;

<snip>
> * * * * i_PIDlist->push_back(tmpPID);
> * * * * //delete tmpPID;
> * * * * //tmpPID=NULL;
>
> * * * * strPID = xmlPIDData.GetNext();
> * * }
>
> * * return true;}
>
> But there's two problems i'm seeing just right out of the bat:
> If the outer while loop goes twice, how would tmpPID be re-declared?


These is no need to do anything special.
If the outer loop iterates multiple times, the brace-enclosed body of
the loop is entered and exited multiple times. At each entry, the
local variables are created anew by the compiler, and at each exit of
the loop body, they are destroyed.

So, if your loop iterates twice, then you will have two distinct
objects named tmpPID, with one being created after the other.

> I thought about declaring it on the heap and then calling delete in
> the end of the funtion before it loops around but even then,
> I would do a i_PIDlist->push_back(tmpPID) and it woudl push on a
> pionter that gets ereased soon after...
>
> Thanks for a little help and guidance here.
>
> Ron


Bart v Ingen Schenau


All times are GMT. The time now is 03:11 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.