简体   繁体   中英

C++ iterator return type for Matrix class using boost::adaptors

I am trying to traverse a custom Matrix, by row and column and use iterators to traverse the data.

I have a working code in the main file but I found difficulties translating it into the Matrix class because I can't seem to figure out the return type/command for the boost::begin iterator. Here is the working code:

#include <iostream>
#include <algorithm>                        //std::transform
#include "Matrix.h"                         //Matrix class
#include <boost/range/algorithm.hpp>        //boost::begin, boost::end
#include <boost/range/adaptor/strided.hpp>  //boost::adaptor::strided
#include <boost/range/adaptor/sliced.hpp>   //boost adaptor::slice


int main(int argc, char *argv[]){

    //creats a matrix with 5 rows and 3 colunms filled with ints from 0 to 14
    //stores the data internaly as a std::vector<T>
    Matrix<int, 5, 3> a = Matrix<int,5,3>(range<int>(15));

    //Matrix is accordingly overloaded, prints out the matrix 
    std::cout << a << std::endl;
    //returns an iterator to the second element and then traverses the vector by skipping 5 elments
    auto begin = boost::begin(boost::adaptors::stride(
                                  boost::adaptors::slice(a.as_vector(), 1, 15), 5));

    //returns an iterator to the end of the vector 
    auto end = boost::begin(boost::adaptors::stride(
                                boost::adaptors::slice(a.as_vector(), 1, 15), 5));

    //multiplies the second column times 2    
    std::transform(begin, end, begin, 
                   [](int i) { return 2*i;});

    //print result
    std::cout << a << std::endl;
}

running the program returns:

$ ./main
[0,]   0   1   2 
[1,]   3   4   5 
[2,]   6   7   8 
[3,]   9  10  11 
[4,]  12  13  14 

[0,]   0   2   2 
[1,]   3   8   5 
[2,]   6  14   8 
[3,]   9  20  11 
[4,]  12  26  14 

so as we see, it works. However the implementation of the begin/end inside the matrix class is causing problems, specifically because I need to replace "auto" by, well I really don't know what.

class Matrix{
    //private data members
    //contuctors, functions
    //returns an iterator to the first element of vector
    typename std::vector<T>::iterator Begin(){
        return matrix.Begin();
    }

    typename std::vector<T>::iterator End(){
        return matrix.End();
    }

    //@ matrix: std::vector<T> that holds that data
    //@ fun - size(): returns the number of values stored in the matrix
    //@ fun - rows(): returns the number of rows of the matrix
    //@ param - i: column to iterate through
    typename ...return type in question... begin_col( std::size_t i ){
        return boost::begin(boost::adaptors::stride(
                                  boost::adaptors::slice( matrix, i, size() ), rows() ) );
            }

    typename ...return type in question... end_col( std::size_t i ){
        return boost::end(boost::adaptors::stride(
                                  boost::adaptors::slice( matrix, i, size() ), rows() ) );
            }
//many more overloaded operators and functions
//end of class
};

So I want begin_col() and end_col() to behave pretty much the same way as the two std::vector::iterator only for the strided slices.

The rest of the matrix class can be found here stackoverflow.com/questions/26282847/c-implementing-iterators-for-custom-matrix-class

Finally the code is compiled by: g++ -Wall -O3 -std=c++11 -o main main.cpp Matrix.cpp

on an ubuntu 14.04.

Any comments are greatly appreciated.

Thank you

Vincent

It took me a while but here is a working code:

 ... certeris paribus

    auto begin_col( size_type i ){
        auto begin = boost::begin(boost::adaptors::stride(
                                boost::adaptors::slice(matrix.get_data(), i, size()), cols()) );
        return begin;
            }

    auto end_col( size_type i ){
        return boost::end(boost::adaptors::stride(
                                boost::adaptors::slice(matrix.get_data(), i, size()), cols()) );
            }

comiling does through a warning:

make -k 
g++ -Wall -O3 -std=c++11 -o main main.cpp Matrix.cpp Vector.cpp
In file included from main.cpp:5:0:
Matrix.h:116:33: warning: ‘begin_col’ function uses ‘auto’ type specifier without trailing return type [enabled by default]
 auto begin_col( size_type i ){

I don't know if I should care or not, but I am happy with the result since:

//main.cpp ...
std::transform(a.begin_col(1), a.end_col(1), a.begin_col(1), []( int i ){ return 2*i; });
std::cout << a << std::endl;
//... 

returns:

$ ./main
[0,]   0   2   2 
[1,]   3   8   5 
[2,]   6  14   8 
[3,]   9  20  11 
[4,]  12  26  14 

--- End of test ---

so that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM