[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

STL Container Question

Posted on 1998-07-04
10
Medium Priority
?
367 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 120 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

834 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