Solved

STL Container Question

Posted on 1998-07-04
10
335 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 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