[英]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. 我在主文件中有一个工作代码,但是在将其转换为Matrix类时遇到困难,因为我似乎无法弄清楚boost :: begin迭代器的返回类型/命令。 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. 但是,在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
};
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. 因此,我希望begin_col()和end_col()的行为与两个std :: vector :: iterator的行为几乎相同,仅适用于跨步切片。
The rest of the matrix class can be found here stackoverflow.com/questions/26282847/c-implementing-iterators-for-custom-matrix-class 其余矩阵类可在此处找到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 最终,代码被编译为:g ++ -Wall -O3 -std = c ++ 11 -o main main.cpp Matrix.cpp
on an ubuntu 14.04. 在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. 就是这样了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.