I am currently working on a linear algebra library (custom vector and matrix plus some algorithms) for educational purposes and personal use. I tried to implement a column itera
I found a solution to the problem. It's a combination of:
this: Get each nth element of iterator range and this: http://www.codeproject.com/Questions/331444/How-to-use-boost-filter-iterator-as-a-class-member
In the end, there was no way around boost. The solution is very simple and looks like:
template
struct EveryNth {
bool operator()(const U& ) { return m_count++ % N == 0; }
EveryNth(std::size_t i) : m_count(0), N(i) {}
private:
int m_count;
std::size_t N;
};
class Matrix{
// code here
typedef boost::filter_iterator,
typename std::vector::iterator> FilterIter;
FilterIter begin_jump(std::size_t i){
return boost::make_filter_iterator >(EveryNth(i), data.begin(), data.end());
}
FilterIter end_jump(std::size_t i){
return boost::make_filter_iterator >(EveryNth(i), data.end(), data.end());
}
};
and the main:
int main(int argc, char *argv[]){
std::vector b = {1,2,3,4,5,6,7,8,9};
MVector a = MVector(b);
for_each(a.begin_jump(2), a.end_jump(2),
[](int i){std::cout << i << " " ;}
);
std::cout << std::endl;
return 0;
}
bash-3.2$ ./main
1 3 5 7 9
or using a.begin_jump(3) instead of two:
bash-3.2$ ./main
1 4 7
which is exactly the intended result.