STL Container Question

Posted on 1998-07-04
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.  

Question by:scs1
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Accepted Solution

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;


Author Comment

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?

LVL 11

Expert Comment

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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

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.

Author Comment

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.
LVL 22

Expert Comment

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.
LVL 11

Expert Comment

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

LVL 11

Expert Comment

ID: 1167169

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().

Author Comment

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.
LVL 11

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse. How to move up/down source folders 4 110
eclipse formatting 6 125
Recommendation vb6 to or others 14 237
How to Correctly derive class from CWinThread in MFC 4 112
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project ( automates most of the tasks discussed in this article. You can even fin…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

738 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