Solved

STL Container Question

Posted on 1998-07-04
10
322 Views
Last Modified: 2013-12-14
I was informed recently that I should use a STL container (vector, list, or deque) instead of a linked list which  I had chosen over a multidimensional array.  I have read about STL containers and they seem to be vergood.  However, have yet to see an example of hhow one might use a deque in place of a multidimensional array.  It seems easy enough to use in place of a 1 dimensional array, but I need help with multidimensional data storage.  Please include some code to explain this to me.  

Thanks.
0
Comment
Question by:scs1
10 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 40 total points
ID: 1167162
In order to simulate a multidimentional array, you can use a container of containers (E.g., vector of vectors).

    vector<vector<int> > multiVec; // The space between the '>' marks is important!
    multiVec[0][0] = 17;

0
 

Author Comment

by:scs1
ID: 1167163
What if I am not sure of the dimension size that I will need prior to program execution. For instance, I may need a 3 dimensional data storage.  This is one of the reasons I am not using arrays. I want to be able to expand the dimension during execution.  Do you have any ideas?

My program wil consist of sort of a tree trunk with many branches on it.  Therefore I need at least a 2 dimensional storage element.  However, the branches may have branches and so on.  Any idea?

Thanks
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167164
Are you sure a multidimentional array is your best bet?  STL provides flexible associative arrays (map<> and hash_map<>) that allow you to define your own access keys.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1167165
I am not sure I understand. A tree is very different from a multi-dimentional array. Maybe you can tell us more what you need it for, so we can understand what it is exactly you need.
0
 

Author Comment

by:scs1
ID: 1167166
I think you misunderstood me.  My reference to a tree was not some element type in C.  I was referring to an actual tree.  I was trying to show how I may need 2, 3, 4, etc. dimensions for my program.  I will not know how many until execution.  I have been thinking long and hard about this, and the only thing I know to do is go back to what I started.  I began making a class called linklist which contains pointers to structures containing the data I need.  With proper use of pointers I should be able to do this.  I was having trouble with this so I placed a question on this site Friday which has led me in the direction of STL containers.  However, it seems as though you must know the dimension of storage needed prior to the start of the program with STL containers.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 1167167
It is somewhat unusual (but not unheard of) to not know the number of dimensions of the array at compile time.  That is why alexo and yonat are trying to get a better idea of EXACTLY what  you are trying to do.  In a case like this, we need to have a very good idea of your goal or out suggestions are likely to do more harm than good.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167168
How would you access elements if you don't know the number of dimentions?
What do you store in your array?

0
 
LVL 11

Expert Comment

by:alexo
ID: 1167169
Wait...

Are you talking about a variable number of dimentions (as in X[n1], X[n1][n2], X[n1][n2][n3], ...) or a variable dimentions size (as in X[n1], X[n2], X[n3])?

Your reference to a linked list suggest the latter.
If so, an STL vector<> is resizable at run-time.  Either by automatically growing the vector when adding elemnts using push_back() and insert(), or by preallocating using reserve() and resize().
0
 

Author Comment

by:scs1
ID: 1167170
I am referring to variable number of dimensions.  However, I have been successful this morning with the task by using the class that I created with several pointers to make a linked list.  I use the new command with the pointers to create however many dimensions I need.  I know that I will need at least 2 dimensions.  I may need three or four dimensions.  However, this is based on user input.  You see I am using MFC to make a schematic representation of a power system.  The user needs to be able to click on each element in the schematic and enter data.  Since the system may have many branches in it.  I need many dimensions when using an array.  However, I have managed to better understand the use of pointers to be able to use them to replicate the many dimensions needed when using an array.  I still have some work to do, and if I run into more trouble then I will post another question.  Thank you for the time and effort you have given.  One good return for me with the communication I have had over this question is that I am now familiar with STL containers.  I see the benefits that the use of them contians.  However, I think for my application I need a user-defined storage method to meet my specific needs.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167171
If you use a linked list, you can use the STL list<> template which gives you a doubly linked list.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now