|
-
September 7th, 2006, 02:02 AM
#1
STL vector - structure's vector
Hi guys,
I have a structure and a vector to a structure.
Code:
struct myParams
{
double angleOX;
double angleOY;
double angleOZ;
double trOX;
double trOY;
double trOZ;
double bandHeight;
};
------------
------------
std::vector<myParams> m_c_Vector;
And I use it into a code like that:
Code:
m_c_Vector.clear();
myParams s_param;
int x = 0;
do {
x = fscanf(file_stream,"%lf\t%lf\t%lf", &s_param.angleOX, &s_param.angleOY, &s_param.angleOZ);
m_c_Vector.push_back(s_param);
} while (x != 0);
Finaly, I have a structure's vector with some valid values on this used members but, some members sets by default, unemployed.
As you can see in the last code section, I'm not using all the members the myParams structure (e.g. trOX, trOY, trOZ, bandHeight). At that moment, I don't need to use this structure's members, but perhaps in the future, I'll need it. I designed myStruct right on this ideea.
I'm asking if that kind of design and writing code it's better than the designing of a smaller structure with the moment necessary members?
Regards.
Last edited by Maximus_X; September 7th, 2006 at 05:41 AM.
-
September 7th, 2006, 07:02 AM
#2
Re: Structure's vector
 Originally Posted by Maximus_X
I'm asking if that kind of design and writing code it's better than the designing of a smaller structure with the moment necessary members?
Thinking ahead, how do you think it would help you in anyway if your structure contained more than it needs?
Also, are you sure that in the event that the structure needs a new member, it will of the "type" that has been added by you in reserve? Not necessarily so - right?
So, the way I see it, things are best left to a simplest minimum.
However, I do realize that you are saving / loading the structure from a file and reverse compatibility may be a potential issue. To resolve this in a better fashion, you can perhaps save a version attribute as say the first line in the file. Now, future versions will be able to handle files created by different previous versions of your software in accordance with their respective idiosyncrasies.
-
September 7th, 2006, 07:07 AM
#3
Re: [RESOLVED] STL vector - structure's vector
If you are not making the file (that you are using for input or output) accomodate the new variables that could be added now, then just adding them into the structure and hence the vector would not help much.
If the file has got those extra fields, then I don't think there's any problem keeping them in your struct as well (as long as you don't worry about that wasted memory).
Also, it would be better if you had overloads wherever such dependency is in your code. The code that is sensitive to the count of fields like the fscanf - should be wrapped into another function and should be overload - so that at a later point of time you could just turn a "switch" to use the other overload and things would be just fine. (Also, it gives you flexibility to "switch" back to older format). It will avoid re-work caused by such "switching".
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
September 7th, 2006, 10:15 AM
#4
Re: Structure's vector
I'd only create the data members needed and risk the compile time down the road should future needs change. The longer I program the more I realize I can't future proof my apps.
By the way, might want to recheck the condition at the end of your loop. Should fscanf return an error you'll not like the result.
-
September 7th, 2006, 12:23 PM
#5
Re: Structure's vector
dynamic allocation
Kuphryn
-
September 8th, 2006, 07:32 AM
#6
Re: Structure's vector
 Originally Posted by Siddhartha
Thinking ahead, how do you think it would help you in anyway if your structure contained more than it needs?
Also, are you sure that in the event that the structure needs a new member, it will of the "type" that has been added by you in reserve? Not necessarily so - right?
So, the way I see it, things are best left to a simplest minimum.
From my experience, sometimes it's better to design a bigger informations data structure than just necessary.
For instance, in database structes design, many engineers prefer to design more fields than that necessary, with generic names. That was one of the reason of this implementation.
Well, in may case, I know that my application, in the future, it may needs this extended members.
In other ideeas order, I guess that you're ideea "things are best left to a simplest minimum" it's better. In fact, if I maintain this extended structers, the extended members will uses memory in vain.
I'm sure that you understand me, and why I was irresolute.
 Originally Posted by Siddhartha
However, I do realize that you are saving / loading the structure from a file and reverse compatibility may be a potential issue. To resolve this in a better fashion, you can perhaps save a version attribute as say the first line in the file. Now, future versions will be able to handle files created by different previous versions of your software in accordance with their respective idiosyncrasies.
Please extend the versions ideea. I don't know exacly what do you mean.
Thanks for your answer.
Kind regards.
-
September 8th, 2006, 07:42 AM
#7
Re: Structure's vector
 Originally Posted by CorpulentCoder
I'd only create the data members needed and risk the compile time down the road should future needs change. The longer I program the more I realize I can't future proof my apps.
By the way, might want to recheck the condition at the end of your loop. Should fscanf return an error you'll not like the result.
I modified the code as you wish and works fine. In fact, without that modifications I met problems with empty files.
Code:
myLPSParams s_param;
char garbage[128];
int x = 0, y = 0;
do {
x = fscanf(file_stream,"%lf\t%lf\t%lf\t%lf", &s_param.angleOX, &s_param.angleOY, &s_param.angleOZ, &s_param.bandHeight);
y += x;
fgets(garbage, 128, file_stream);
m_c_Vector.push_back(s_param);
m_sl_MAXWrongImagesCounter++;
} while ((x != 0) && ( y != -1));
Thanks for your advice.
 Originally Posted by kuphryn
dynamic allocation
Kuphryn
Man, I don't understand what do you mean with dynamic allocation in this case.
Last edited by Maximus_X; September 8th, 2006 at 07:47 AM.
-
September 8th, 2006, 07:43 AM
#8
Re: Structure's vector
 Originally Posted by Maximus_X
Please extend the versions ideea. I don't know exacly what do you mean.
Lets me explain it with an example.
Say, you are storing personal information. At this moment, all you need to work with is Name, Address, and Phone Number, and therefore, every struct PersonalInfo is defined (in the initial release) as -
Code:
struct PersonalInfo
{
std::string strName;
std::string strAddress;
std::string strTelephoneNumber;
};
You persisted this information for a 100 people. However, tomorrow, specifications change and you need to persist (and read) Bank Details and the Stocks these people possess. For one, you still need to be able to read the files created using the older, less advanced system - how do you go about doing this?
One way would be to persist version information. i.e. Every time you develop or change the way data is written to the file, you increment the version, and save a file with the version information as a header. While, reading you would first read the file version, and if it was 1.0, you would know that this file contains only names, address and phone numbers. However, file version "2.0" would be treated as one with account information, and stock activity, et al. Hope you got the hang of it.
Another recommendation in this case would be to use XML for an information storage format. If every attribute in the structure PersonalInfo was to be an XML Tag, like this -
Code:
<Records>
<PersonalInfo>
<Name>Someone</Name>
<Address>Somewhere</Address>
<TelephoneNumber>Something</TelephoneNumber>
</PersonalInfo>
<PersonalInfo>
<Name>Another one</Name>
<Address>Elsewhere</Address>
<TelephoneNumber>Another thing</TelephoneNumber>
</PersonalInfo>
</Records>
...You could easily add more elements to your structure in later versions, and still maintain backward compatibility.
So, XML would be my most preferred way of saving information.
Last edited by Siddhartha; September 8th, 2006 at 07:50 AM.
-
September 8th, 2006, 07:52 AM
#9
Re: Structure's vector
I have updated my post above with a sample XML. FYI.
-
September 10th, 2006, 02:03 PM
#10
Re: Structure's vector
First of all, thank you very much for this explications. I understand very well what you meet and what you recomand me, too.
 Originally Posted by Siddhartha
Lets me explain it with an example.
Say, you are storing personal information. At this moment, all you need to work with is Name, Address, and Phone Number, and therefore, every struct PersonalInfo is defined (in the initial release) as -
Code:
struct PersonalInfo
{
std::string strName;
std::string strAddress;
std::string strTelephoneNumber;
};
You persisted this information for a 100 people. However, tomorrow, specifications change and you need to persist (and read) Bank Details and the Stocks these people possess. For one, you still need to be able to read the files created using the older, less advanced system - how do you go about doing this?
One way would be to persist version information. i.e. Every time you develop or change the way data is written to the file, you increment the version, and save a file with the version information as a header. While, reading you would first read the file version, and if it was 1.0, you would know that this file contains only names, address and phone numbers. However, file version "2.0" would be treated as one with account information, and stock activity, et al. Hope you got the hang of it.
Yes! I understand very well your's explications and I got the hang of it. It's a very nice and professional point of view.
 Originally Posted by Siddhartha
Another recommendation in this case would be to use XML for an information storage format. If every attribute in the structure PersonalInfo was to be an XML Tag, like this -
Code:
<Records>
<PersonalInfo>
<Name>Someone</Name>
<Address>Somewhere</Address>
<TelephoneNumber>Something</TelephoneNumber>
</PersonalInfo>
<PersonalInfo>
<Name>Another one</Name>
<Address>Elsewhere</Address>
<TelephoneNumber>Another thing</TelephoneNumber>
</PersonalInfo>
</Records>
...You could easily add more elements to your structure in later versions, and still maintain backward compatibility.
So, XML would be my most preferred way of saving information.
I know and understand the XML ideea. But in my case it was not necessary. I just need to export this dates in a log file.
I appreciate your's ideeas.
Kind Regards.
-
September 10th, 2006, 02:16 PM
#11
Re: [RESOLVED] STL vector - structure's vector
 Originally Posted by exterminator
If you are not making the file (that you are using for input or output) accomodate the new variables that could be added now, then just adding them into the structure and hence the vector would not help much.
If the file has got those extra fields, then I don't think there's any problem keeping them in your struct as well (as long as you don't worry about that wasted memory).
Also, it would be better if you had overloads wherever such dependency is in your code. The code that is sensitive to the count of fields like the fscanf - should be wrapped into another function and should be overload - so that at a later point of time you could just turn a "switch" to use the other overload and things would be just fine. (Also, it gives you flexibility to "switch" back to older format). It will avoid re-work caused by such "switching".
First of all, thank you very much for your's complete explications.
I appreciate very much your's ideeas. It's a professional point of view and that's will be my point of view, too.
Kind regards.
Last edited by Maximus_X; November 7th, 2006 at 03:52 PM.
-
September 10th, 2006, 02:24 PM
#12
Re: [RESOLVED] STL vector - structure's vector
[ merged threads ]
Regards,
Siddhartha
-
September 10th, 2006, 02:28 PM
#13
Re: [RESOLVED] STL vector - structure's vector
 Originally Posted by Siddhartha
[ merged threads ]
Regards,
Siddhartha
I don't know how... First, I was going to delete the thread from C++ forum, but I didn't find how...
Show me, how can I merge the thread.
Regards!
-
September 10th, 2006, 02:31 PM
#14
Re: [RESOLVED] STL vector - structure's vector
 Originally Posted by Maximus_X
First, I was going to del the thread from C++ forum, but I didn't find how...
Just click the "Edit" button on the first post and you will find an option to delete that post. Deleting the first post will delete the thread.
(Please don't delete this thread - but, you can test the forum here!)
 Originally Posted by Maximus_X
Show me, how can I merge the thread.
Ah, as far as merging goes - you can ask me...
-
September 11th, 2006, 02:56 AM
#15
Re: [RESOLVED] STL vector - structure's vector
 Originally Posted by Siddhartha
Just click the "Edit" button on the first post and you will find an option to delete that post. Deleting the first post will delete the thread.
(Please don't delete this thread - but, you can test the forum here!)
Thanks for that explication. Be sure that I'll never delete this thread.
I'm glad that I can start interesting and usedfull threads.
All the best!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|