I'm working on a program that takes elements from a user and sorts them. For this program, I have to use a vector as the size of the element list is unknown prior to user input. Our instructions were:
Write a program in C++ to implement sorting of a list of elements. Elements can be of any type but will all be of same type, like all integers or all floats or all chars or all strings (strings shall be sorted like in a dictionary). You can implement any sorting algorithm of your choice.
- Ask the user how many elements will be there
- Ask the user to input elements
- Ask the user to choose the sorting order: ascending or descending or both
- Print both input and output lists
- User will not provide any information regarding the type of elements
I'm not very familiar with vectors (teacher basically skimmed topic in class) and my book isn't giving me a whole lot of information on the subject. The problem I'm running into is that I don't know the type of the element list until the user begins input. So far, I have tried:
- creating a void type vector (obviously not allowed now that I've researched it, oops)
- overloading a function called
insertInVectorby sending the first element to the function and letting the function determine which vector type to create based on the type of the first element (which seemed like my best option when I thought of it, except I need access to the vector after the function terminates, so that ended up being a no go, too) #include <typeinfo>in program, finding the type of the first element, and then creating a vector usingvector<typeid(firstElement).name()>and honestly I'm not sure why that didn't work, but it didn't.
Like I said I have EXTREMELY limited experience with vectors as this is my first time using them. I'm also a fa