gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:52:09 25 4
gpt4 key购买 nike

我正在尝试按行和列遍历自定义矩阵,并使用迭代器遍历数据。

我在主文件中有一个工作代码,但我发现很难将它翻译成 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

正如我们所见,它有效。然而,矩阵类中开始/结束的实现导致了问题,特别是因为我需要将“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 ---

就这样吧。

关于使用 boost::adaptors 的 Matrix 类的 C++ 迭代器返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26534916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com