?
Solved

STL Container Question

Posted on 1998-07-04
10
Medium Priority
?
365 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

650 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