Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > struct containing vector?

Reply
Thread Tools

struct containing vector?

 
 
cerr
Guest
Posts: n/a
 
      02-01-2010
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
 
Reply With Quote
 
 
 
 
cerr
Guest
Posts: n/a
 
      02-01-2010
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();
}
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      02-03-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Inline initialization of a struct containing a string array Antti Karanta C Programming 4 03-29-2008 05:33 PM
Intializing struct containing an union Michael Brennan C Programming 4 08-07-2006 08:24 AM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
How to call an external API function that takes a struct containing arrays as its parameter? Robert Rossney ASP .Net 0 09-01-2003 05:51 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57