0

I want to insert multiple array using Vector of vector.
For that I/P is like this:

2   : Number of array ( can be vary )    

3  1 2 3  :  3( size of array)  1 2 3( elements in array )      

4  4 5 6 7  : 4( size of array) 4 5 6 7 ( elements in array)       

Then I need to perform some operation on it.
All the I/P is entered by user. Only its range is given.

2 <= No of array <= 10 


1 <= Elements in array <= 1000000 

For above I tried following ways :

int main()
{
    int no_of_array, size;
    long int num;
    int j = 0; 

    cin >> no_of_array;

1st way :

vector < vector<int> > array_vector[no_of_array];
   while( no_of_array -- )
   {
        cin >>size;
        for( int i = 0; i < size ; i++ )
            {
                cin >> num;
                array_vector[j].push_back({num});
            }
            j++;
    }
  cout<< vector_array[1][2];    // Error 

2nd way :

vector< vector<int> > array_vector;
while( no_of_array --)
{
    cin >> size;
    for( int i = 0 ; i < size ; i++ )
    {
        cin >> num;
        array_vector[j].push_back(num);
    }
    j++; 
 }   
cout<< vector_array[1][2];     // Error       

3rd way : ( Working )

vector<vector<int> > array_vector;
for( int i = 0 ; i < no_of_array ; i++ )
{
    cin >> size;
    vector<int> v;
    for( int j = 0 ; j < size ; j++ )
    {

        cin >> num;
        v.push_back(num);
    }
    array_vector.push_back(v);
}    
cout<<array_vector[1][2];    // No Error         

Somewhere I read 3rd way of vector of vector is not suitable for competitive programming. Why ??
What mistake I did while traversing the elements with 1st two approach ??

Tushar
  • 97
  • 4
  • 11
  • 4
    "... is not suitable for competitive programming..." - so what? Those "competitive programming" sites don't care about best practices or writing maintainable code. Don't waste your time on them. You are just going to learn bad habits and stuff that you'll most likely never use in the real world anyway. Write *real* programs and read [good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead. – Jesper Juhl Jul 09 '17 at 15:24
  • @ Jesper Juhl real programming ??? Please , can you give example ?? – Tushar Jul 09 '17 at 15:33
  • 2
    I mean write real programs that you actually *need* or *want* to write, instead of solutions to stupid problems from competitive programming sites. – Jesper Juhl Jul 09 '17 at 15:42

1 Answers1

1

You can try the following code

int main(int argc,char* argv[])
{
    unsigned int num;
    int elm;
    unsigned int size;

    std::cout << "Enter the number of rows" << std::endl;
    std::cin >> num;
    std::vector<std::vector<int> > a(num,std::vector<int>(0,0));

    for(unsigned int i=0;i<num;i++)
    {
        std::cout  << "Enter the size of " << i+1 << "array:" << std::endl;
        std::cin >> size;

        for(unsigned int j=0;j<size;j++)
        {
            std::cout << "Enter the element" <<std::endl;
            std::cin >> elm;

            a[i].push_back(elm);
        }
    }

    for(unsigned int i=0;i<a.size();i++)
    {
        for(unsigned int j=0;j<a[i].size();j++)
        {
            std::cout << "Element at [" <<i<<"][" <<j <<"] is " << a[i][j] <<std::endl;
        }
    }

    return 0;
}
Harry
  • 2,177
  • 1
  • 19
  • 33