Posted on 16 Aug 2022, this text provides information on Learning Aids/Tools related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.
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 inputinto the container. Instead I decided to move that math to other functions and just take a single inputinto 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//Needed for std::cout and std::endl#include//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 writtentemplate<class myType>classSimpleContainer{public://I think there are other ways to do the following but I don't know themSimpleContainer(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 positionvoid setValue(int containerPosition, myType inputValue);//I feel like I'm missing a reference here but & threw an errorvoidinputEntireVector(std::vectorinputVector);//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 viewvoid printContainer();private:
std::vector classVector;};template<class myType>intSimpleContainer::getSize(){return classVector.size();}template<class myType>voidSimpleContainer::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 inputvalueint numInputCopies =1;
classVector.insert(classVector.begin()+ containerPosition, numInputCopies,inputValue);}template<class myType>voidSimpleContainer::inputEntireVector(std::vectorinputVector){
classVector.swap(inputVector);}template<class myType>
myType SimpleContainer::getValue(int containerPosition){
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
2 years ago
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.