473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

14
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<NVPairsWithId> 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
Dec 6 '07 #1
5 2020
weaknessforcats
9,208 Expert Mod 8TB
If your buffer has to have entries like:

id c1 c2


why not just use:
Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.    string id;
  4.    string name;
  5.    string value;
  6. };
  7.  
and then just have a list<Data> ??
Dec 6 '07 #2
rsennat
14
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

If your buffer has to have entries like:

id c1 c2


why not just use:
Expand|Select|Wrap|Line Numbers
  1. struct Data
  2. {
  3.    string id;
  4.    string name;
  5.    string value;
  6. };
  7.  
and then just have a list<Data> ??
Dec 6 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
id=100, {name = c1, value = r1}, {name = c2, value = r1}.
I see.

Then in that case your approach will work:
list<NVPairsWithId> m_ResponseObj;

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

struct NVObject
{
string m_name;
string m_value;
};
Expand|Select|Wrap|Line Numbers
  1. list<NVPairsWithId> m_ResponseObj;
  2. NVPairsWithId id100;
  3. id100.m_id = 100;
  4. m_ResponseObj.puch_back(id100);
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. NVPairsWithId* ptr = FindMyId(100); //detail of searxch omitted
  2. NVObject data;
  3. data.name = "Ralph";
  4. data.value = "45";
  5. ptr->m_NVPairs.push_back(data);
  6.  
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++.
Dec 6 '07 #4
rsennat
14
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

I see.

Then in that case your approach will work:


Expand|Select|Wrap|Line Numbers
  1. list<NVPairsWithId> m_ResponseObj;
  2. NVPairsWithId id100;
  3. id100.m_id = 100;
  4. m_ResponseObj.puch_back(id100);
  5.  
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:
Expand|Select|Wrap|Line Numbers
  1. NVPairsWithId* ptr = FindMyId(100); //detail of searxch omitted
  2. NVObject data;
  3. data.name = "Ralph";
  4. data.value = "45";
  5. ptr->m_NVPairs.push_back(data);
  6.  
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++.
Dec 6 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Dec 6 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
3
by: Jef Driesen | last post by:
The number of items in my std::list is changed after sorting (using std::list member function sort). I'm using MSVC6 and the actual (pseudo) code is posted below. The output from size() is...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
15
by: desktop | last post by:
If I have a sorted std::list with 1.000.000 elements it takes 1.000.000 operations to find element with value = 1.000.000 (need to iterator through the whole list). In comparison, if I have a...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.