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 ??

3
  • 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 instead. Commented Jul 9, 2017 at 15:24
  • @ Jesper Juhl real programming ??? Please , can you give example ?? Commented Jul 9, 2017 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. Commented Jul 9, 2017 at 15:42

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.