Click to See Complete Forum and Search --> : storing paralel data - BEST way


dalitg
September 2nd, 2002, 03:27 AM
Hi,
I need to store a list of names [surname, first, title].
What objects are best for saving this data ?
1. list per each component [array for surname, first, title ].
2. array of structures of Names [struct including surname, first, title]
3. STL way.
4. ELSE?

Thanks!

cup
September 2nd, 2002, 04:18 AM
Either (2) or (3). Depends on how you wish to initialize it, access it and how fast the access should be. Even with (3), there are a whole bunch of containers. The one you choose depends on how you wish to access the data more than anything else.

Yves M
September 2nd, 2002, 08:48 AM
A fourth way would be to store it in a database. This in case you have a lot of data and need to be able to do advanced things with it. Like looking for all people who have the same title, while also being able to quickly look up people's title based on surname and first name.

BTW, I would include something like a unique ID for a person, since there might be two people with the same name and title.

jfaust
September 2nd, 2002, 09:53 AM
2 and 3 would be best:


struct Name
{
Name() {}
Name(std::string f, std::string m, std::string l)
: First(f), Middle(m), Last(l) {}

std::string First;
std::string Middle;
std::string Last;
};

// typedefs for simplification
typedef std::vector<Name> NameList;
typedef NameList::iterator NameListIter;
typedef NameList::const_iterator NameListConstIter;

int main()
{
NameList n;
n.push_back(Name("John", "H.", "Doe"));

for( NameListIter iter = n.begin(); iter != n.end(); ++iter )
{
Name& name = *iter;
// do what you need
}

return 1;
}


Using a standard container gives you access to many algorithms that would be useful, such as std::find(). typedefing the container is useful because it 1) simplifies the code, and 2) gives you one place to change if you need to change the container type.

Jeff

dalitg
September 11th, 2002, 08:37 AM
Hi Jfaust ,
Thanks for your code, and for advise.
I started to use STL list container, and made a class inherited publically from it.

I. it seems to me there are little differences between vector & list container, regarding algorithms, do you have any idea?

II. How is CArray of CString instead of STL? Better/worse

Thanks anyway.
Dalit:)

Yves M
September 11th, 2002, 08:45 AM
1. Well, the big advantage of std::vector over std::list is that a vector has guaranteed constant time random access to elements. It also guarantees contiguous storage of elements in case you might need that.

2. It has to be worse :p It's only implemented for MFC, so If you ever decide to reuse code for a) a project that doesn't use MFC (such as ATL, or Win32 API, or console) or b) a different platform / compiler, then CString and CArray won't be available.

dalitg
September 11th, 2002, 08:49 AM
Hi Jeff,

Thanks.
I will take your views into concideration & into projects.
Bye,
Dalit

Yves M
September 11th, 2002, 08:52 AM
Originally posted by dalitg
Hi Jeff,

Thanks.
I will take your views into concideration & into projects.
Bye,
Dalit

Is everybody here called Jeff, or were you replying to Jeff a second time ? :D

dalitg
September 11th, 2002, 08:59 AM
Ok Yves, i admit noticing that you replied, and not Jfaust.
so,:D.
Bye,
P.S. Yves , nice footer- I will let you know what does it mean.
Dalit
:)

Graham
September 11th, 2002, 09:35 AM
Originally posted by dalitg
Hi Jfaust ,
Thanks for your code, and for advise.
I started to use STL list container, and made a class inherited publically from it.

I. it seems to me there are little differences between vector & list container, regarding algorithms, do you have any idea?

II. How is CArray of CString instead of STL? Better/worse

Thanks anyway.
Dalit:)
Don't inherit publically from STL containers - they're not designed for it.

There are massive differences between vector and list. Get hold of Effective STL by Scott Meyers for the full story. One major difference is that you (generally) can't pass a list to any of the algorithms - that's why list has member functions to do the same job.

CArray and CString don't even come close, IMHO.