Skip to main content
Rolled back post to remove new off-topic request; removed noise
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Need advice with a simple Simple container class with templates C++ (code layout and readability)

First of all, thanks for looking at this code. I'm a novice coder trying to learn C++ on my own so any help is much appreciated.

Anyway, I really hope that the answers here are helpful to others, and I appreciate the time of anyone that posts!

#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();
}

Based on recommendations below I modified the code but I'm getting the following errors.

|8|warning: friend declaration 'myType operator<<(std::ostream&, const SimpleContainer&)' declares a non-template function Note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

|88|undefined reference to `operator<<(std::ostream&, SimpleContainer const&)'

I know I'm probably making mistakes here. I spent a bunch of time trying to find out how to format this properly but nothing that I found worked. Here is the modified code:

#include <iostream>
#include <vector>
#include <stddef.h>

template<class myType>
class SimpleContainer
{
friend myType operator<<(std::ostream& stream, const SimpleContainer& object);
public:
    SimpleContainer(std::size_t xDim, std::size_t yDim, myType const& defaultValue = myType())
        : objectData(xDim * yDim, defaultValue)
        , xSize(xDim)
        , ySize(yDim)
    {}

    //These are overloaded operators for () for the class which allows for the syntax:
    //Matrix  m(2,3);   // 2D array size 2,3 (default constructed).
    //m(4,5) = 6;       // Will modify the container version to be 6
    myType&       operator()(std::size_t x, std::size_t y)       {return objectData[y*xSize + x];}
    myType const& operator()(std::size_t x, std::size_t y) const {return objectData[y*xSize + x];}

    //Simply looks at the classVector size and returns (used for iterating over it)
    int getSize()
    {
        return objectData.size();
    }

    //Passing by value instead of reference since it uses swap
    void inputEntireVector(std::vector<myType> inputVector)
    {
        objectData.swap(inputVector);
    }

    //Not currently functioning but does compile when not used with std::cout
    void printContainer(std::ostream& stream);

private:
    std::vector<myType> objectData;
    std::size_t          xSize;
    std::size_t          ySize;
};

template<class myType>
inline void SimpleContainer<myType>::printContainer(std::ostream& stream)
{

    for(int i = 0; i < objectData.size(); i++)
    {
        stream << objectData[i];
    }

    // Or you can use std::copy()
    // std::copy(classVector.begin(), classVector.end(),
    //            std::ostream_iterator<type>(stream, ""/*No Space*/));
}

template<class myType>
myType inline operator<<(std::ostream& stream, const SimpleContainer<myType>& object)
{
    object.printContainer(stream);
    return stream;
}

//Runs some basic interactions with the Container
void sampleContainerInterfacing();

int main()
{
    sampleContainerInterfacing();
    return 0;
}

void sampleContainerInterfacing()
{
    //I couldn't figure out how to usefully put these inside the class
    static int const ConsoleWidth  = 80;
    static int const ConsoleHeight = 25;
    size_t width  = ConsoleWidth;
    size_t height = ConsoleHeight;
    SimpleContainer<int> mySimpleContainer(width, height, 0);

    std::cout << mySimpleContainer.getSize() << std::endl;

    mySimpleContainer(0,0) = 5;
    std::cout << mySimpleContainer(0,0) << std::endl;

    //This is the main thing that's not working properly
    std::cout << mySimpleContainer;
}

I had to use the friend function inside the class and use myType in front of the operator<< definition because otherwise the compiler gave the following errors:

|60|error: passing 'const SimpleContainer' as 'this' argument of 'void SimpleContainer::printContainer(std::ostream&) [with myType = int]' discards qualifiers

|58|error: ISO C++ forbids declaration of 'operator<<' with no type

Hopefully these aren't completely stupid questions! Thanks in advance for your time.

Need advice with a simple container class with templates C++ (code layout and readability)

First of all, thanks for looking at this code. I'm a novice coder trying to learn C++ on my own so any help is much appreciated.

Anyway, I really hope that the answers here are helpful to others, and I appreciate the time of anyone that posts!

#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();
}

Based on recommendations below I modified the code but I'm getting the following errors.

|8|warning: friend declaration 'myType operator<<(std::ostream&, const SimpleContainer&)' declares a non-template function Note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

|88|undefined reference to `operator<<(std::ostream&, SimpleContainer const&)'

I know I'm probably making mistakes here. I spent a bunch of time trying to find out how to format this properly but nothing that I found worked. Here is the modified code:

#include <iostream>
#include <vector>
#include <stddef.h>

template<class myType>
class SimpleContainer
{
friend myType operator<<(std::ostream& stream, const SimpleContainer& object);
public:
    SimpleContainer(std::size_t xDim, std::size_t yDim, myType const& defaultValue = myType())
        : objectData(xDim * yDim, defaultValue)
        , xSize(xDim)
        , ySize(yDim)
    {}

    //These are overloaded operators for () for the class which allows for the syntax:
    //Matrix  m(2,3);   // 2D array size 2,3 (default constructed).
    //m(4,5) = 6;       // Will modify the container version to be 6
    myType&       operator()(std::size_t x, std::size_t y)       {return objectData[y*xSize + x];}
    myType const& operator()(std::size_t x, std::size_t y) const {return objectData[y*xSize + x];}

    //Simply looks at the classVector size and returns (used for iterating over it)
    int getSize()
    {
        return objectData.size();
    }

    //Passing by value instead of reference since it uses swap
    void inputEntireVector(std::vector<myType> inputVector)
    {
        objectData.swap(inputVector);
    }

    //Not currently functioning but does compile when not used with std::cout
    void printContainer(std::ostream& stream);

private:
    std::vector<myType> objectData;
    std::size_t          xSize;
    std::size_t          ySize;
};

template<class myType>
inline void SimpleContainer<myType>::printContainer(std::ostream& stream)
{

    for(int i = 0; i < objectData.size(); i++)
    {
        stream << objectData[i];
    }

    // Or you can use std::copy()
    // std::copy(classVector.begin(), classVector.end(),
    //            std::ostream_iterator<type>(stream, ""/*No Space*/));
}

template<class myType>
myType inline operator<<(std::ostream& stream, const SimpleContainer<myType>& object)
{
    object.printContainer(stream);
    return stream;
}

//Runs some basic interactions with the Container
void sampleContainerInterfacing();

int main()
{
    sampleContainerInterfacing();
    return 0;
}

void sampleContainerInterfacing()
{
    //I couldn't figure out how to usefully put these inside the class
    static int const ConsoleWidth  = 80;
    static int const ConsoleHeight = 25;
    size_t width  = ConsoleWidth;
    size_t height = ConsoleHeight;
    SimpleContainer<int> mySimpleContainer(width, height, 0);

    std::cout << mySimpleContainer.getSize() << std::endl;

    mySimpleContainer(0,0) = 5;
    std::cout << mySimpleContainer(0,0) << std::endl;

    //This is the main thing that's not working properly
    std::cout << mySimpleContainer;
}

I had to use the friend function inside the class and use myType in front of the operator<< definition because otherwise the compiler gave the following errors:

|60|error: passing 'const SimpleContainer' as 'this' argument of 'void SimpleContainer::printContainer(std::ostream&) [with myType = int]' discards qualifiers

|58|error: ISO C++ forbids declaration of 'operator<<' with no type

Hopefully these aren't completely stupid questions! Thanks in advance for your time.

Simple container class with templates

#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();
}
Tweeted twitter.com/#!/StackCodeReview/status/230078204751249408
added 4191 characters in body
Source Link
bazola
  • 8.6k
  • 2
  • 34
  • 73

Based on recommendations below I modified the code but I'm getting the following errors.

|8|warning: friend declaration 'myType operator<<(std::ostream&, const SimpleContainer&)' declares a non-template function Note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

|88|undefined reference to `operator<<(std::ostream&, SimpleContainer const&)'

I know I'm probably making mistakes here. I spent a bunch of time trying to find out how to format this properly but nothing that I found worked. Here is the modified code:

#include <iostream>
#include <vector>
#include <stddef.h>

template<class myType>
class SimpleContainer
{
friend myType operator<<(std::ostream& stream, const SimpleContainer& object);
public:
    SimpleContainer(std::size_t xDim, std::size_t yDim, myType const& defaultValue = myType())
        : objectData(xDim * yDim, defaultValue)
        , xSize(xDim)
        , ySize(yDim)
    {}

    //These are overloaded operators for () for the class which allows for the syntax:
    //Matrix  m(2,3);   // 2D array size 2,3 (default constructed).
    //m(4,5) = 6;       // Will modify the container version to be 6
    myType&       operator()(std::size_t x, std::size_t y)       {return objectData[y*xSize + x];}
    myType const& operator()(std::size_t x, std::size_t y) const {return objectData[y*xSize + x];}

    //Simply looks at the classVector size and returns (used for iterating over it)
    int getSize()
    {
        return objectData.size();
    }

    //Passing by value instead of reference since it uses swap
    void inputEntireVector(std::vector<myType> inputVector)
    {
        objectData.swap(inputVector);
    }

    //Not currently functioning but does compile when not used with std::cout
    void printContainer(std::ostream& stream);

private:
    std::vector<myType> objectData;
    std::size_t          xSize;
    std::size_t          ySize;
};

template<class myType>
inline void SimpleContainer<myType>::printContainer(std::ostream& stream)
{

    for(int i = 0; i < objectData.size(); i++)
    {
        stream << objectData[i];
    }

    // Or you can use std::copy()
    // std::copy(classVector.begin(), classVector.end(),
    //            std::ostream_iterator<type>(stream, ""/*No Space*/));
}

template<class myType>
myType inline operator<<(std::ostream& stream, const SimpleContainer<myType>& object)
{
    object.printContainer(stream);
    return stream;
}

//Runs some basic interactions with the Container
void sampleContainerInterfacing();

int main()
{
    sampleContainerInterfacing();
    return 0;
}

void sampleContainerInterfacing()
{
    //I couldn't figure out how to usefully put these inside the class
    static int const ConsoleWidth  = 80;
    static int const ConsoleHeight = 25;
    size_t width  = ConsoleWidth;
    size_t height = ConsoleHeight;
    SimpleContainer<int> mySimpleContainer(width, height, 0);

    std::cout << mySimpleContainer.getSize() << std::endl;

    mySimpleContainer(0,0) = 5;
    std::cout << mySimpleContainer(0,0) << std::endl;

    //This is the main thing that's not working properly
    std::cout << mySimpleContainer;
}

I had to use the friend function inside the class and use myType in front of the operator<< definition because otherwise the compiler gave the following errors:

|60|error: passing 'const SimpleContainer' as 'this' argument of 'void SimpleContainer::printContainer(std::ostream&) [with myType = int]' discards qualifiers

|58|error: ISO C++ forbids declaration of 'operator<<' with no type

Hopefully these aren't completely stupid questions! Thanks in advance for your time.

Based on recommendations below I modified the code but I'm getting the following errors.

|8|warning: friend declaration 'myType operator<<(std::ostream&, const SimpleContainer&)' declares a non-template function Note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here)

|88|undefined reference to `operator<<(std::ostream&, SimpleContainer const&)'

I know I'm probably making mistakes here. I spent a bunch of time trying to find out how to format this properly but nothing that I found worked. Here is the modified code:

#include <iostream>
#include <vector>
#include <stddef.h>

template<class myType>
class SimpleContainer
{
friend myType operator<<(std::ostream& stream, const SimpleContainer& object);
public:
    SimpleContainer(std::size_t xDim, std::size_t yDim, myType const& defaultValue = myType())
        : objectData(xDim * yDim, defaultValue)
        , xSize(xDim)
        , ySize(yDim)
    {}

    //These are overloaded operators for () for the class which allows for the syntax:
    //Matrix  m(2,3);   // 2D array size 2,3 (default constructed).
    //m(4,5) = 6;       // Will modify the container version to be 6
    myType&       operator()(std::size_t x, std::size_t y)       {return objectData[y*xSize + x];}
    myType const& operator()(std::size_t x, std::size_t y) const {return objectData[y*xSize + x];}

    //Simply looks at the classVector size and returns (used for iterating over it)
    int getSize()
    {
        return objectData.size();
    }

    //Passing by value instead of reference since it uses swap
    void inputEntireVector(std::vector<myType> inputVector)
    {
        objectData.swap(inputVector);
    }

    //Not currently functioning but does compile when not used with std::cout
    void printContainer(std::ostream& stream);

private:
    std::vector<myType> objectData;
    std::size_t          xSize;
    std::size_t          ySize;
};

template<class myType>
inline void SimpleContainer<myType>::printContainer(std::ostream& stream)
{

    for(int i = 0; i < objectData.size(); i++)
    {
        stream << objectData[i];
    }

    // Or you can use std::copy()
    // std::copy(classVector.begin(), classVector.end(),
    //            std::ostream_iterator<type>(stream, ""/*No Space*/));
}

template<class myType>
myType inline operator<<(std::ostream& stream, const SimpleContainer<myType>& object)
{
    object.printContainer(stream);
    return stream;
}

//Runs some basic interactions with the Container
void sampleContainerInterfacing();

int main()
{
    sampleContainerInterfacing();
    return 0;
}

void sampleContainerInterfacing()
{
    //I couldn't figure out how to usefully put these inside the class
    static int const ConsoleWidth  = 80;
    static int const ConsoleHeight = 25;
    size_t width  = ConsoleWidth;
    size_t height = ConsoleHeight;
    SimpleContainer<int> mySimpleContainer(width, height, 0);

    std::cout << mySimpleContainer.getSize() << std::endl;

    mySimpleContainer(0,0) = 5;
    std::cout << mySimpleContainer(0,0) << std::endl;

    //This is the main thing that's not working properly
    std::cout << mySimpleContainer;
}

I had to use the friend function inside the class and use myType in front of the operator<< definition because otherwise the compiler gave the following errors:

|60|error: passing 'const SimpleContainer' as 'this' argument of 'void SimpleContainer::printContainer(std::ostream&) [with myType = int]' discards qualifiers

|58|error: ISO C++ forbids declaration of 'operator<<' with no type

Hopefully these aren't completely stupid questions! Thanks in advance for your time.

Source Link
bazola
  • 8.6k
  • 2
  • 34
  • 73

Need advice with a simple container class with templates C++ (code layout and readability)

First of all, thanks for looking at this code. I'm a novice coder trying to learn C++ on my own so any help is much appreciated.

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.

Anyway, I really hope that the answers here are helpful to others, and I appreciate the time of anyone that posts!

#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();
}