dimanche 2 juillet 2017

Pushing elements from a 2d to 1d vector

I am trying to push elements from a 2d integer matrix(created using vector of vectors) into a 1d integer vecto.While trying to push back if I do:

 //declarations
 //vector<vector<int>>nums
 //vector<int>ans;int a
 for(int i=0;i<nums.size();i++)
    { 
        for(int j=0;j<nums[i].size();j++)
        {        
                arr.push_back(nums[i,j]);
        }
    }

I am getting an error as no matching function call. However if I do this:

 for(int i=0;i<nums.size();i++)
    { 
        for(int j=0;j<nums[i].size();j++)
        {       a=nums[i,j]; 
                arr.push_back(a);    
        }
    }

It works. I understand that ,nums[][] is getting read as a vector,but shouldn't nums[i][j] which is an integer be read as it is and not raise an error. Can somebody explain the difference in both the cases?

Aucun commentaire:

Enregistrer un commentaire