C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to create this data structure?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
wanna sandwich
Guest





PostPosted: Fri Sep 09, 2005 3:24 pm    Post subject: how to create this data structure? Reply with quote



Hi,

I'm using Visual C++ and I'm trying to create a global data structure that
looks like this:

mainarray[1] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString

mainarray[2] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString

etc...

I need to be able to add and remove from this data structure from the
various methods within the main cpp implementation file. Any advice on how
to create this?

Thanks!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Christopher Yeleighton
Guest





PostPosted: Sat Sep 10, 2005 12:45 am    Post subject: Re: how to create this data structure? Reply with quote




"wanna sandwich" <wannasandwich (AT) hotmail (DOT) com> wrote

Quote:
Hi,

I'm using Visual C++ and I'm trying to create a global data structure that
looks like this:
[snip]


Do you want to define the structure itself or an instance of the structure?

Chris



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Ali Çehreli
Guest





PostPosted: Sat Sep 10, 2005 1:33 pm    Post subject: Re: how to create this data structure? Reply with quote



"wanna sandwich" <wannasandwich (AT) hotmail (DOT) com> wrote


Quote:
I'm using Visual C++ and I'm trying to create a global data structure that
looks like this:

mainarray[1] =

To be consistent and conventional, I recommend you switch to 0-based
indexing:

mainarray[0]

Quote:
subarray1[1] = subarray2[CString, CString, CString, ...];

I recommend to dropping CString, and using std::string. For the above type,
std::vector should work:

#include <string>
#include <vector>

/* ... */

typedef std::vector<std::string> Names;

Quote:
subarray1[2] = CString
subarray1[3] = CString

Do subarray1 really represent a collection; or do mainarray actually contain
three members? I will assume the latter, and give this as a beginning:

class Parent
{
Names children_;
std::string name_;
std::string last_name_;

public:

Parent(std::string const & name, std::string const & last_name);

void add_child(std::string const & name);

/* ... */
};

Then you need a collection of those:

typedef std::vector<Parent> Parents;

int main()
{
Parents parents;

Parent ali("Ali", "Somebody");
ali.add_child("Bora");
parents.push_back(ali);

// maybe even later:
parents[0].add_child("Can");
}

Ali


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
NMail
Guest





PostPosted: Sat Sep 10, 2005 1:33 pm    Post subject: Re: how to create this data structure? Reply with quote

by using a 'struct'

struct MainArray
{
vector<string> SubArray1;
string s1;
string s2;
};





wanna sandwich wrote:
Quote:
Hi,

I'm using Visual C++ and I'm trying to create a global data structure that
looks like this:

mainarray[1] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString

mainarray[2] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString

etc...

I need to be able to add and remove from this data structure from the
various methods within the main cpp implementation file. Any advice on how
to create this?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
wanna sandwich
Guest





PostPosted: Sat Sep 10, 2005 1:37 pm    Post subject: Re: how to create this data structure? Reply with quote

"Christopher Yeleighton" <krixel (AT) qed (DOT) pl> wrote

Quote:

"wanna sandwich" <wannasandwich (AT) hotmail (DOT) com> wrote in message
news:3E3Ue.450053$s54.155183 (AT) pd7tw2no (DOT) ..
Hi,

I'm using Visual C++ and I'm trying to create a global data structure
that
looks like this:
[snip]

Do you want to define the structure itself or an instance of the
structure?

Chris

I guess I just need an instance of the structure. I was thinking that I will
probably need to create a class for this, but I'm still a little foggy on
how that would work.

Thanks.

Darren


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
BobR
Guest





PostPosted: Sat Sep 10, 2005 1:37 pm    Post subject: Re: how to create this data structure? Reply with quote


wanna sandwich wrote in message <3E3Ue.450053$s54.155183@pd7tw2no>...
Quote:
Hi,

I'm using Visual C++ and I'm trying to create a global data structure that
looks like this:

mainarray[1] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString
mainarray[2] =
subarray1[1] = subarray2[CString, CString, CString, ...];
subarray1[2] = CString
subarray1[3] = CString
etc...
I need to be able to add and remove from this data structure from the
various methods within the main cpp implementation file. Any advice on how
to create this?
Thanks!

#include<vector>
using std::vector;
// vector<vector > mainarray;

typedef vector<std::string> vSub2;
typedef vector<vSub2> vSub1;
vector<vSub1> mainarray;

std::string HiThere("Hello Vector!");
vSub2 Sub2;
vSub1 Sub1;
Sub2.push_back(HiThere);
Sub1.push_back(Sub2);
mainarray.push_back(Sub1);

vSub2 Sub2b(3); // reserve 3
vSub1 Sub1b(3);
Sub2b.at(0)=HiThere;
Sub2b.at(1)=HiThere;
Sub2b.at(2)=HiThere;
Sub1b.at(0)=Sub2b;
Sub1b.at(1)=Sub2b;
Sub1b.at(2)=Sub2b;
mainarray.push_back(Sub1b);

std::cout<<"mainarray.at(0).at(0).at(0)
="< std::cout<<"mainarray.at(1).at(2).at(2)
="<
// output:
mainarray.at(0).at(0).at(0) =Hello Vector!
mainarray.at(1).at(2).at(2) =Hello Vector!

--
Bob R
POVrookie


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.