Why Is C++ Saying My Vector Doesnt Have a Puch Back Member
C++ investigation: Arrays vs Vectors
These posts are designed with the goal of learning more about Modern C++ and looking at some of the conventional wisdom that I've always taken for granted. The first post will be about challenging some of that conventional wisdom, looking at an underlying implementation choice. The next post will look at using some Modern C ++ and adding Concepts to the library. A common piece of advice in programming is to use the standard library. However, C++'s standard library has often come under scrutiny for being too general, and can often be beaten by hand-crafted solutions.
Goal of these posts
These posts are designed with the goal of learning more about Modern C++, and looking at some of the conventional wisdom that I've always taken for granted. I'm going to start off by building a matrix library and see where it goes. This first post will be about challenging some of that conventional wisdom and looking at an underlying implementation choice. The next post I'll look at using some Modern C++ and adding Concepts to the library.
Overview of the Idea
One thing a matrix library needs to do is represent matrices of different sizes. In this case, it is ideal to have the matrix object backed by a dynamically sized container. Ideally this container has fast access times meaning that a container such as a linked list is not ideal for this scenario. Of the common data structures arrays and hash tables fit this criteria, both having O(1) access times. Since it is possible to map each item in a matrix to a unique index (its location), an array type data structure makes sense for the underlying container.
A common piece of advice in programming is to use the standard library. However, C++'s standard library has often come under scrutiny for being too general, and due to that generality, can often be beaten by hand-crafted solutions. Let's see if for a basic matrix implementation, if there is any difference between using a pointer to an array on a heap and the standard library's vector type.
The Code
The code for the two matrices can be seen here and below
#ifndef __MATRIX2D_HPP__
#define __MATRIX2D_HPP__
#include <vector>
template <typename T>
class MatrixVec2D
{
std::vector<T> mat;
std::size_t rows;
std::size_t cols;
std::size_t rcToIdx(std::size_t r, std::size_t c) const
{
return cols * r + c;
}
public:
MatrixVec2D<T>(std::size_t r, std::size_t c) :
mat(r*c, 0),
rows(r),
cols(c)
{
}
MatrixVec2D<T>(const MatrixVec2D<T> & m):
mat(m.mat),
rows(m.rows),
cols(m.cols)
{
}
std::size_t getNumRows() const
{
return rows;
}
std::size_t getNumCols() const
{
return cols;
}
const T& at(std::size_t row, std::size_t col) const
{
return mat[rcToIdx(row, col)];
}
T& at(std::size_t row, std::size_t col)
{
return mat[rcToIdx(row, col)];
}
};
template <typename T>
class MatrixArr2D
{
T * mat;
std::size_t rows;
std::size_t cols;
std::size_t rcToIdx(std::size_t r, std::size_t c) const
{
return cols * r + c;
}
public:
MatrixArr2D<T>(std::size_t r, std::size_t c):
mat(new T[r*c]),
rows(r),
cols(c)
{
for(auto i = 0; i < r*c; ++i)
{
mat[i] = 0;
}
}MatrixArr2D<T>(const MatrixArr2D<T> & m):
mat(new T[m.rows * m.cols]),
rows(m.rows),
cols(m.cols)
{
for(auto i = 0; i < rows*cols; ++i)
{
mat[i] = m.mat[i];
}
}~MatrixArr2D()
{
delete [] mat;
}std::size_t getNumRows() const
{
return rows;
}
std::size_t getNumCols() const
{
return cols;
}
const T& at(std::size_t row, std::size_t col) const
{
return mat[rcToIdx(row, col)];
}
T& at(std::size_t row, std::size_t col)
{
return mat[rcToIdx(row, col)];
}
};
0 Response to "Why Is C++ Saying My Vector Doesnt Have a Puch Back Member"
Post a Comment