How to create vectors?

What is the difference between an array of vector and a vector of vectors?

  • More specifically I am trying to create a array of vectors where each vector has been allocated  a certain amount of space say MAX. This is how I am implementing this.. vector<int> v(MAX)[10] // creating 10 vectors where each vector has MAX space reserved... But this is giving me a syntax error.. I can successfully implement the same if it were a vector of vector like this vector< vector<int> > v(10,vector<int>(MAX))... but this allocation is very slow if I put a large number instead of 10!! So my question is how to implement an array of reserved vectors?? I am doing all this just to create an optimized version of a vector adjacency list ( this is in turn for a question where I need to implement a heavily optimized version of Dijsktra).....so will all this really help in optimizing Dijsktra?

  • Answer:

    Difference : Vector of vector means 2D array with variable  size in both direction . where as in array of vector means only one is having variable size . You can use following approach to declare . warning : I didn't check for Efficiency . struct Array{ int X[MAX];  //As you already know it's size }; vector<Array> myvec; or Use std::array ( >= C++11 )  array<array<int, MAX>, 10> Myarray or int Array[MAX][10] As you already know how many elements will be there in vector and how many vector you need . or  map< int , Array> mymap; or map<int , vector<int> > Mymap; As you asked for 10! .Generally in any function you can't declare int[10!][MAX] or int[MAX][10!] . In that case you should use map(mymap) of struct  or map of vector (Mymap) .To declare such a huge space you need to declare them as Global variable .

Sonu Kumar at Quora Visit the source

Was this solution helpful to you?

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.