'Iterator for Matrix Native class class
I wrote my own Matrix class with such fields (you can't use STL containers in it)
template <typename T>
class Matrix
{
private:
T *data = nullptr;
size_t rows;
size_t cols;
I also made an iterator for this class:
public:
class Iterator
{
friend Matrix;
private:
T *curr;
public:
Iterator() : curr(nullptr) {}
Iterator(Matrix *matrix) : curr(matrix->data) {}
~Iterator() = default;
......
Iterator begin()
{
Iterator it(this);
std::cout << *it.curr << std::endl;
return it;
}
Iterator end()
{
Iterator it(this);
/*Iterator it(this->data + rows * cols);
if (it == nullptr)
{
return it;
}*/
return it;
}
How can I properly write an end() method that would return the next element after the last one? I understand that using address arithmetic you can get the latter, but how can I do it in my code?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
