LINUX.ORG.RU

История изменений

Исправление KennyMinigun, (текущая версия) :

Чисто ради практического интереса использования фичей С++14 я тут накостылял обёртку для std::vector в виде stdx::matrix (пока без изменения размера как самостоятельной матрицы):

http://ideone.com/93dmgH

template<typename T, typename Allocator = std::allocator<T>>
class matrix_helper {
public:
    friend class matrix<T, Allocator>;

    using o_matrix = matrix<T, Allocator>;
    using p_vector = std::vector<T, Allocator>;
    using v_size_t = typename p_vector::size_type;

    typename p_vector::reference operator [] (v_size_t colindex) {
        return static_cast<p_vector &>(matrix_)[rowindex_ * matrix_.rowsize() + colindex];
    }

    operator typename p_vector::reference () {
        return static_cast<p_vector &>(matrix_)[rowindex_];
    }

    typename p_vector::reference operator = (T &&value) {
        return (typename p_vector::reference)(*this) = std::forward<T>(value);
    }

protected:
    matrix_helper(o_matrix &matrix, v_size_t rowindex) :
        matrix_(matrix),
        rowindex_(rowindex)
    {}
...
};

template <typename T, typename Allocator = std::allocator<T>>
class matrix : public std::vector<T, Allocator> {
public:
    using p_vector = std::vector<T, Allocator>;
    using v_size_t = typename p_vector::size_type;

    matrix(std::initializer_list<std::initializer_list<T>> contents)
    {
        for (auto &row : contents) {
            rowsize_ = row.size();
            p_vector::insert(p_vector::end(), row);
        }
    }

    v_size_t rowsize() const { return rowsize_; }

    matrix_helper<T, Allocator> operator [] (v_size_t rowindex) {
        return matrix_helper<T, Allocator>(*this, rowindex);
    }

private:
    v_size_t rowsize_ = invalid_size;
};

Исходная версия KennyMinigun, :

Чисто ради практического интереса использования фичей С++14 я тут накостылял обёртку для std::vector в виде stdx::matrix:

http://ideone.com/93dmgH

template<typename T, typename Allocator = std::allocator<T>>
class matrix_helper {
public:
    friend class matrix<T, Allocator>;

    using o_matrix = matrix<T, Allocator>;
    using p_vector = std::vector<T, Allocator>;
    using v_size_t = typename p_vector::size_type;

    typename p_vector::reference operator [] (v_size_t colindex) {
        return static_cast<p_vector &>(matrix_)[rowindex_ * matrix_.rowsize() + colindex];
    }

    operator typename p_vector::reference () {
        return static_cast<p_vector &>(matrix_)[rowindex_];
    }

    typename p_vector::reference operator = (T &&value) {
        return (typename p_vector::reference)(*this) = std::forward<T>(value);
    }

protected:
    matrix_helper(o_matrix &matrix, v_size_t rowindex) :
        matrix_(matrix),
        rowindex_(rowindex)
    {}
...
};

template <typename T, typename Allocator = std::allocator<T>>
class matrix : public std::vector<T, Allocator> {
public:
    using p_vector = std::vector<T, Allocator>;
    using v_size_t = typename p_vector::size_type;

    matrix(std::initializer_list<std::initializer_list<T>> contents)
    {
        for (auto &row : contents) {
            rowsize_ = row.size();
            p_vector::insert(p_vector::end(), row);
        }
    }

    v_size_t rowsize() const { return rowsize_; }

    matrix_helper<T, Allocator> operator [] (v_size_t rowindex) {
        return matrix_helper<T, Allocator>(*this, rowindex);
    }

private:
    v_size_t rowsize_ = invalid_size;
};