 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
wanna sandwich Guest
|
Posted: Fri Sep 09, 2005 3:24 pm Post subject: how to create this data structure? |
|
|
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
|
Posted: Sat Sep 10, 2005 12:45 am Post subject: Re: how to create this data structure? |
|
|
"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
|
Posted: Sat Sep 10, 2005 1:33 pm Post subject: Re: how to create this data structure? |
|
|
"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
|
Posted: Sat Sep 10, 2005 1:33 pm Post subject: Re: how to create this data structure? |
|
|
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
|
Posted: Sat Sep 10, 2005 1:37 pm Post subject: Re: how to create this data structure? |
|
|
"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
|
Posted: Sat Sep 10, 2005 1:37 pm Post subject: Re: how to create this data structure? |
|
|
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 |
|
 |
|
|
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
|
|