how to build string buffer table from std::list and structs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rsennat
    New Member
    • Dec 2007
    • 14

    how to build string buffer table from std::list and structs

    Hi,
    I'm having the following structures and std::list of that structs.

    I need to read the list of list as below and build a buffer as a table information.
    Any idea on how to build a table info (string buffer).

    list<NVPairsWit hId> m_ResponseObj;

    struct NVPairsWithId
    {
    string m_id;
    list<NVObject> m_NVPairs;
    };

    struct NVObject
    {
    string m_name;
    string m_value;
    };

    This is how the data in it will be, (kind of each and every row information)
    m_id=100
    m_name=c1, m_name=c2
    m_value=r1, m_value=r1

    m_id=200
    m_name=c1, m_name=c2
    m_value=r2, m_value=r2

    This, I need to traverse the list of list and build the buffer as
    id c1 c2
    100 r1 r1
    200 r2 r2

    Please help me out on this.
    Thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    If your buffer has to have entries like:

    id c1 c2


    why not just use:
    [code=cpp]
    struct Data
    {
    string id;
    string name;
    string value;
    };
    [/code]

    and then just have a list<Data> ??

    Comment

    • rsennat
      New Member
      • Dec 2007
      • 14

      #3
      It's not like that. For a single id, there could be multiple sets of names and values. here for id=100, {name = c1, value = r1}, {name = c2, value = r1}.
      so it goes like that....

      id c1 c2
      ------------
      100 r1 r1
      200 r2 r2

      Any idea how can i build this..
      thanks

      Originally posted by weaknessforcats
      If your buffer has to have entries like:

      id c1 c2


      why not just use:
      [code=cpp]
      struct Data
      {
      string id;
      string name;
      string value;
      };
      [/code]

      and then just have a list<Data> ??

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by rsennat
        id=100, {name = c1, value = r1}, {name = c2, value = r1}.
        I see.

        Then in that case your approach will work:
        Originally posted by rsennat
        list<NVPairsWit hId> m_ResponseObj;

        struct NVPairsWithId
        {
        string m_id;
        list<NVObject> m_NVPairs;
        };

        struct NVObject
        {
        string m_name;
        string m_value;
        };
        [code=cpp]
        list<NVPairsWit hId> m_ResponseObj;
        NVPairsWithId id100;
        id100.m_id = 100;
        m_ResponseObj.p uch_back(id100) ;
        [/code]

        That gets the ids in m_ResponseObj.

        Then to add a name/value pair you will need to traverse m_ResponseObj looking for the element whose m_id is, say 100, then you add the name/value pair to that element:
        [code=cpp]
        NVPairsWithId* ptr = FindMyId(100); //detail of searxch omitted
        NVObject data;
        data.name = "Ralph";
        data.value = "45";
        ptr->m_NVPairs.push _back(data);
        [/code]

        More or less. I didn't compile or test this code.

        Lastly, avoid naming conventions, like m_, for your variables. A member variable is this->name. The compiler can't check that everyone is using m_. Microsoft started this by having C programmers write C++.

        Comment

        • rsennat
          New Member
          • Dec 2007
          • 14

          #5
          That's the way I'm doing it for putting the data into this.
          But I would like to read the data back from it to build the string buffer table (tabular format).

          Thanks

          Originally posted by weaknessforcats
          I see.

          Then in that case your approach will work:


          [code=cpp]
          list<NVPairsWit hId> m_ResponseObj;
          NVPairsWithId id100;
          id100.m_id = 100;
          m_ResponseObj.p uch_back(id100) ;
          [/code]

          That gets the ids in m_ResponseObj.

          Then to add a name/value pair you will need to traverse m_ResponseObj looking for the element whose m_id is, say 100, then you add the name/value pair to that element:
          [code=cpp]
          NVPairsWithId* ptr = FindMyId(100); //detail of searxch omitted
          NVObject data;
          data.name = "Ralph";
          data.value = "45";
          ptr->m_NVPairs.push _back(data);
          [/code]

          More or less. I didn't compile or test this code.

          Lastly, avoid naming conventions, like m_, for your variables. A member variable is this->name. The compiler can't check that everyone is using m_. Microsoft started this by having C programmers write C++.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            I would think you could iterate m_ResponseObj and for each element you traverse m_NVPairs.

            When you reach the end of m_NVPairs, you advance to the next element of m_ResponseObj. When you reach the end of m_ResponeObj, you are done.

            The display format should be easy.

            Comment

            Working...