I am trying to fill a vector with a matrix of values in c++. I'm not very self confident with this procedure (I don't know well about pointers and I don't know if I need it here) however I am trying this
When I try to compile I receive an error which says
invalid arguments 'Candidates are: void push_back(const int &)
Can anyone tell me wether it's possible to do, how can I solve it?
I read something about erasing cache memory, changing my eclipse compiler, my c++ version, however I don't think the problem is so big.
Best How To :
You cannot push back a matrix into a vector. What you can do is preallocate memory for your vector (for speeding things up) then use the
std::vector<>::assign member function to "copy" from the matrix into the vector:
vector<int> collectionSum(gray.rows * gray.cols); // reserve memory, faster
collectionSum.assign(*auxMat, *auxMat + gray.rows * gray.cols);
This should be pretty fast. Otherwise, you can push back each individual element in a loop.
See May I treat a 2D array as a contiguous 1D array? for some technicalities regarding possible undefined behaviour (thanks @juanchopanza for the comment). I believe the code is safe, due to the fact that the storage of the matrix is contiguous.