Hello, I have this problem that's been bothering me on the back of my head for so long now. I don't know how to pursue or even break it down, I've tried reading the book, look at notes, nothing helps. Problem is, I have this file which contains

Nitrite N1O2
NitrousOxide N2O1
Nitric N1O1
Nitryl N1O2
list goes on

I've read from the file ( easy ) but the directions for this lab states
to store for each special formula along with names then display it in ascending order. The formulas are to be stored in structs and then placed in vectors. Basically the output should look like this.


Nitrite, Nitryl



It's said to be done "without sorting" and states

Notice that the output is sorted only by the number of nitrogen atoms.
There is no attempt to sort by anything else.
Or, put another way: all other orderings show simply the order the items were taken from the file.

I don't understand how this can be done without sorting. Please help.

Just an idea.... im not sure if this is what you want but, you could create a struct that contains the chemical name and chemical makeup, then create an array of that struct to store file contents, which is easy. But in order to list in ascending order you HAVE to sort through all of the chemicals, I dont understand why sorting is not allowed....

alright thanks, it's better than not getting any feedback =) so basically create an array of structs of that makeup and then sort. Oh we'll guess i'm going to have to ignore what the lab needs.

Any other feedback would help greatly!

All you have to do is read the file but insert the elements you get into
its correct place. Is this enough of a hint?

Isn't a vector automatically sorted? Or am I thinking of something else?

No, a vectors not sorted =(. It has a function to sort stuff I believe but it doesn't look like it's allowed in this lab.

firstPerson - So your saying I have to read the file such as

in >> nitrogenName >> tempChar >> nitrogenNumber >> tempChar >> oxygenNumber;

then place the elements in the correct spot? But how? What If I stored a nitrogen of number 12 and placed it into a struct, then pushed it back a vector, then the next number is 1 and I do the same. It's not like fstream allows me to skip reading some stuff from the file and goes back to it, then I could store it. The lab states "all other orderings show simply the order the items were taken from the file." which makes no sense because i can't simply just read, push back, and display! It doesn't make much sense to me.

Maybe map/multimap is the solution. N2O1 could be used as the key and NitrousOxide as stored value. All elements in map are implicity sorted.

Just my 0.02 but it sounds like it might be a candidate for a singly linked list. You can enter the datum into it's proper place in the chain as it's coming in from the file. That approach is basically sorting the data but it doesn't explicitly take everything into a collection and then sort it.

Well according to this thread I concluded that, theres no other way to solve this problem BUT to use a sort function. Can one more person confirm this?