+ 4

Managing Column Iterators for a Matrix Class

I wished to iterate over the elements of the following class using a range based for loop. https://code.sololearn.com/cq24J951TxgI/?ref=app In a similar thread posted before [https://www.sololearn.com/Discuss/1135525/using-a-range-based-for-loop-with-a-custom-matrix-class], I was told to use a vector of vectors for internal storage instead of the 2D pointer I was using, as those comes readily with begin and end. After declaring the iterator getters, I decided to declare column iterators for the same matrix class as well, as the default begin would only access vectors row wise. The first solution to get column iterators was to declare a separate vector of vectors (assume cmat) and store the transpose of the original data into this cmat, update it whenever the real one will be altered, and return cmat's iterators when asked. Now, I could use these column iterators in functions like this : for_each(M1.col_begin(),M1.col_end(), [](vector<Real> v){for(Real i:v) cout<<i<<" "; cout<<endl;}); /// col_begin() and col_end() are the starting /// iterators and end iterators respectively. Now, I had a function at() to access the matrix element like in any other STL Container. I made it return a reference as I wanted to use this function to alter specific elements as well. M& Matrix<M>::at(uint rc,uint cc) { return (mat.at(rc-1).at(cc-1)); } Now, the problem was that if I tried : Matrix M1(2,2); M1.at(2,2) = 5; The vector cmat will not be altered, as it would never know that some change did occur inside the original vector. Now, how can I update cmat after such operations? I need the column iterators to stay updated whenever I make a change. Please help me! Also, if there is a better way I can manage retrieving column iterators, please let me know. In case you need the partial updated version (replaced pointers with vectors) : https://code.sololearn.com/c4HeCwMv3Kj9/#cpp

22nd Mar 2018, 7:27 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
1 Antwort
+ 10
long and complicated questions. I wonder why usually are not answered...
23rd Mar 2018, 8:41 AM
Vukan
Vukan - avatar