I've been working on a simple program to output values to the console as a learning project, and I stumbled across an article advising against using 2D containers, suggesting to simulate them instead with a 1D vector/container. I immediately proceeded to try to create a ridiculous class that converted input X and Y values into the correct position in the class's vector member. This class had a lot of redundant code, and eventually I realized that I was losing the value of the simplicity of the container having only one dimension by requiring two dimensions to input into the container. Instead I decided to move that math to other functions and just take a single input into the vector inside the class.
The following code is an attempt at a SimpleContainer class. I realize that I am reinventing the wheel here, but this is just a project to aid the learning process for me. I'm trying to learn C++ best practices, and things like good code layout and readability. I know that this class is missing some important optimization and features, and I think I'm doing unnecessary copying, but it would be very helpful for me to have a breakdown of what I'm doing right and wrong. This is also my first experience with using templates, but thankfully the code compiles, so I think I'm doing it right.
#include <iostream> //Needed for std::cout and std::endl
#include <vector> //Needed for std::vector and others
//These values are arbitrary and are only used when computing the
//screen size for things like memory allocation for the Container
#define CONSOLEWIDTH 80;
#define CONSOLEHEIGHT 25;
//The purpose of this class is to have an easy way to input variables and objects into a container,
//sometimes a large number of them at a time, and then retrieve and print them easily as well.
//Templating the class allows the input and printing of ints and chars very easily
//You could input objects, but some of the functions may not work with the code as written
template <class myType>
class SimpleContainer
{
public:
//I think there are other ways to do the following but I don't know them
SimpleContainer(int containerSize, myType defaultValue)
{
classVector.resize(containerSize);
for(int i = 0; i < containerSize; i++)
{
classVector[i] = defaultValue;
}
}
//Simply looks at the classVector size and returns (used for iterating over it)
int getSize();
//The setValue function sets the value at specified container position
void setValue(int containerPosition, myType inputValue);
//I feel like I'm missing a reference here but <myType>& threw an error
void inputEntireVector(std::vector<myType> inputVector);
//You have to compute the X and Y positions outside of this function if you want
//to simulate a 2D matrix
myType getValue (int containerPosition);
//Prints the entire contents of the container starting from vector.begin()
//I think this will only work with ints and chars, and with ints over 9 it will
//mess up the alignment of the console view
void printContainer();
private:
std::vector<myType> classVector;
};
template<class myType>
int SimpleContainer<myType>::getSize()
{
return classVector.size();
}
template<class myType>
void SimpleContainer<myType>::setValue(int containerPosition, myType inputValue)
{
classVector.erase(classVector.begin() + containerPosition);
//vector.insert() takes for its third argument a value for the number of times to
//insert the input value
int numInputCopies = 1;
classVector.insert(classVector.begin() + containerPosition, numInputCopies, inputValue);
}
template<class myType>
void SimpleContainer<myType>::inputEntireVector(std::vector<myType> inputVector)
{
classVector.swap(inputVector);
}
template<class myType>
myType SimpleContainer<myType>::getValue(int containerPosition)
{
return classVector[containerPosition];
}
template<class myType>
void SimpleContainer<myType>::printContainer()
{
for(int i = 0; i < classVector.size(); i++)
{
std::cout << classVector[i];
}
}
//Runs some basic interactions with the Container
void sampleContainerInterfacing();
int main()
{
sampleContainerInterfacing();
return 0;
}
void sampleContainerInterfacing()
{
//Setting integers for the view width and height to input into Container
//Uses them immediately to do the math to figure out Container size
int width = CONSOLEWIDTH;
int height = CONSOLEHEIGHT;
int containerSize = width*height;
SimpleContainer<int> mySimpleContainer(containerSize, 0);
//Outputs one console screen worth of 0's
mySimpleContainer.printContainer();
std::cout << mySimpleContainer.getSize() << std::endl;
//Defining these variables to aid readability of the 2D matrix math
int position;
int posY = 5;
int posX = 7;
//The position in the container equals the width * the desired y position
//plus the desired x position, -1 because it starts counting with 0
position = width * posY + posX - 1;
mySimpleContainer.setValue(position, 5);
std::cout << mySimpleContainer.getValue(position) << std::endl;
//Now contains the input variable
mySimpleContainer.printContainer();
}
m[1][1]. \$\endgroup\$