CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Structure's vector

    Quote 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.

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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".

  4. #4
    Join Date
    Mar 2006
    Posts
    13

    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.

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Structure's vector

    dynamic allocation

    Kuphryn

  6. #6
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Structure's vector

    Quote 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.
    Quote 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.

  7. #7
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: Structure's vector

    Quote 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.

    Quote 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.

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Structure's vector

    Quote 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.

  9. #9
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Structure's vector

    I have updated my post above with a sample XML. FYI.

  10. #10
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    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.

    Quote 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.
    Quote 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.

  11. #11
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: [RESOLVED] STL vector - structure's vector

    Quote 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.

  12. #12
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: [RESOLVED] STL vector - structure's vector

    [ merged threads ]

    Regards,
    Siddhartha

  13. #13
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: [RESOLVED] STL vector - structure's vector

    Quote 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!

  14. #14
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: [RESOLVED] STL vector - structure's vector

    Quote 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!)
    Quote Originally Posted by Maximus_X
    Show me, how can I merge the thread.
    Ah, as far as merging goes - you can ask me...

  15. #15
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: [RESOLVED] STL vector - structure's vector

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured