Sorting Vector Elements (P.668)

  1. In this exercise, you are going to write a program to read input from "/home/solomon/WWW/Lang/C/vector1.dat". You may copy the file to your working directory with the command Then in your program, you can refer to the file by "vector1.dat" only, without the full path.
  2. The length of the data file is unknown, although we do know that in each line there is exactly a string composed of one or two digits. Therefore, you may need to use the skill of "indefinite loop" as in the exercise of Input File Stream. We shall store the strings in a vector, because the capacity of a vector can increase whenever it is necessary.
  3. In the loop, read a string from the data file, and print the string out with a preceding serial number, as shown below.
    
    1:      20
    2:      24
    3:      7
    4:      58
    5:      40
    6:      41
    7:      17
    8:      55
    9:      84
    10:     63
    11:     78
    12:     18
    13:     9
    14:     15
    15:     23
    16:     14
    17:     54
    18:     45
    19:     66
    20:     32
    
    
  4. Next, invoke the sort() function as mentioned in the textbook (P.668) to sort the vector ascendingly. Note that strings are sorted by ASCII-code order, so the result should look like:
    
    1:      14
    2:      15
    3:      17
    4:      18
    5:      20
    6:      23
    7:      24
    8:      32
    9:      40
    10:     41
    11:     45
    12:     54
    13:     55
    14:     58
    15:     63
    16:     66
    17:     7
    18:     78
    19:     84
    20:     9