Speak now
Please Wait Image Converting Into Text...
Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Challenge yourself and boost your learning! Start the quiz now to earn credits.
Unlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
General Tech Learning Aids/Tools 2 years ago
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.
Turn Your Knowledge into Earnings.
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 //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 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 & threw an error void inputEntireVector(std::vector 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 classVector; }; template<class myType> int SimpleContainer::getSize() { return classVector.size(); } template<class myType> void SimpleContainer::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::inputEntireVector(std::vector inputVector) { classVector.swap(inputVector); } template<class myType> myType SimpleContainer::getValue(int containerPosition) { REPLY 0 views 0 likes 0 shares Facebook Twitter Linked In WhatsApp
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.
General Tech 9 Answers
General Tech 7 Answers
General Tech 3 Answers
General Tech 2 Answers
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.