Sorting multiple vectors

In my program i have three sperate vectors that store the students last name, first and score. I need to sort it by last name and print the contents of the vectors lastname, firstname: score. Well i have got it doing that for the most part. But after it sorts the lastnames the first names and scores are still in the same postion in their vectors, so when it prints the reults the last names are with the wrong first name and score. any ideas?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
 #include <iostream>  // allows the program to output data to the screen
#include <conio.h>
#include  <iomanip>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <ios>
#include <istream>
#include <limits>
#include "students.h" // gradebook class defintion


using std::cout; // program uses cout
using std::cin; // program uses cin
using std::endl; // program uses endl
using std::setprecision; // set numeric output precision
using std::fixed; // ensures that decimal point is displayed
using std::setw;
using std::string;
using std::vector;
using std::max;


void  students::displayMessage()
{
        
    cout << endl << "Welcome to the Student Scores Application." << endl << endl;
}
        

void students::getData()
{
    int numStudents = 0;
    string name;	
    int score = 0;
    int i = 0;
    
    cout <<"Enter number of students to enter: ";
    cin >> numStudents;
    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    
    vector<string> student_lastnames;
    vector<string> student_firstnames;
    vector <int> student_score;

    do
   
    { 
            cout << endl << "Student " << i + 1 <<" last name: "; 
            cin >> name;
      	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
	    student_lastnames.push_back(name);
    	    



            cout << "Student " << i + 1 <<" first name: "; 
            cin >> name;
     	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	    student_firstnames.push_back(name);
      
            cout << "Student " << i + 1 <<" score: "; 
            cin >> score;
	    cout << endl;
	    cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
	    student_score.push_back(score);
     		
     i++;
     }

     while ( i < numStudents);
    

            // sort them alphabetically
        sort (student_lastnames.begin(), student_lastnames.end());

    for (int i =0; i < student_lastnames.size(); i++)
    {
            cout << student_lastnames[i] << ", " << student_firstnames[i] << ": " << student_score[i] << endl;
    }
      
    

}

void students::End()
{
	cout << endl << endl << "Press any key to continue..."; // prompts the user to continue or quit 
	char c = getch();
}


// function main begins program exectuion
int main() 
{
    students mystudent;
    
    mystudent.displayMessage();

    mystudent.getData(); 
    
    mystudent.End();	   
    
} 
Hi...
As ur program shows and as I have understood ur problem...

U have taken three different vectors as..

1
2
3
    vector<string> student_lastnames;
    vector<string> student_firstnames;
    vector <int> student_score;


U have pushed data to these vectors.

After Input U have sorted only the vector which contains the lastname only.

sort (student_lastnames.begin(), student_lastnames.end());

After sorting U have displayed the result as...

1
2
3
4
    for (int i =0; i < student_lastnames.size(); i++)
    {
            cout << student_lastnames[i] << ", " << student_firstnames[i] << ": " << student_score[i] << endl;
    }


So U have not made any changes to the vectors ....

1
2
    vector<string> student_firstnames;
    vector <int> student_score;


Thats why the content of these vectors are as it is, where as the vector
vector<string> student_lastnames;
is being sorted.

Using vector may not fulfill ur requirement.
Using map may solve ur problem...
Last edited on
You probably want a vector of structs, eg

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct student {
    // Default constructor will be needed if inserting into vector
    student() : first_name(), last_name(), score() {}

    student( std::string const& f, std::string const& l, int s ) :
        first_name( f ), last_name( l ), score( s ) {}

    // std::sort will require operator< to compile.
    // WARNING: function does not provide a total ordering
    //   (ie, is "Jane Doe" < "John Doe" necessarily given the following code?)
    bool operator<( student const& rhs ) const
       { return last_name < rhs.last_name; }

    std::string first_name;
    std::string last_name;
    int           score;
};


Now have a vector<student> instead of three separate vectors.
Topic archived. No new replies allowed.