簡體   English   中英

使用boost :: adaptors的Matrix類的C ++迭代器返回類型

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

我試圖按行和列遍歷自定義矩陣,並使用迭代器遍歷數據。

我在主文件中有一個工作代碼,但是在將其轉換為Matrix類時遇到困難,因為我似乎無法弄清楚boost :: begin迭代器的返回類型/命令。 這是工作代碼:

#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;
}

運行程序返回:

$ ./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 

因此,正如我們所見,它是有效的。 但是,在matrix類中實現begin / end會引起問題,特別是因為我需要替換“ auto”,所以我真的不知道是什么。

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
};

因此,我希望begin_col()和end_col()的行為與兩個std :: vector :: iterator的行為幾乎相同,僅適用於跨步切片。

其余矩陣類可在此處找到stackoverflow.com/questions/26282847/c-implementing-iterators-for-custom-matrix-class

最終,代碼被編譯為:g ++ -Wall -O3 -std = c ++ 11 -o main main.cpp Matrix.cpp

在Ubuntu 14.04上。

任何意見,不勝感激。

謝謝

文森特

我花了一段時間,但這是一個有效的代碼:

 ... 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()) );
            }

通過警告進行編譯:

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 ){

我不知道是否應該關心,但是由於以下原因,我對結果感到滿意:

//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;
//... 

返回:

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

--- End of test ---

就是這樣了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM