473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>  // allows the program to output data to the screen
  2. #include <conio.h>
  3. #include  <iomanip>
  4. #include <cstdlib>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <numeric>
  9. #include <fstream>
  10. #include <ios>
  11. #include <istream>
  12. #include <limits>
  13. #include "students.h" // gradebook class defintion
  14.  
  15.  
  16. using std::cout; // program uses cout
  17. using std::cin; // program uses cin
  18. using std::endl; // program uses endl
  19. using std::setprecision; // set numeric output precision
  20. using std::fixed; // ensures that decimal point is displayed
  21. using std::setw;
  22. using std::string;
  23. using std::vector;
  24. using std::max;
  25.  
  26.  
  27. void  students::displayMessage()
  28. {
  29.  
  30.     cout << endl << "Welcome to the Student Scores Application." << endl << endl;
  31. }
  32.  
  33.  
  34. void students::getData()
  35. {
  36.     int numStudents = 0;
  37.     string name;    
  38.     int score = 0;
  39.     int i = 0;
  40.  
  41.     cout <<"Enter number of students to enter: ";
  42.     cin >> numStudents;
  43.     cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  44.  
  45.     vector<string> student_lastnames;
  46.     vector<string> student_firstnames;
  47.     vector <int> student_score;
  48.  
  49.     do
  50.  
  51.     { 
  52.             cout << endl << "Student " << i + 1 <<" last name: "; 
  53.             cin >> name;
  54.               cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
  55.         student_lastnames.push_back(name);
  56.  
  57.  
  58.  
  59.  
  60.             cout << "Student " << i + 1 <<" first name: "; 
  61.             cin >> name;
  62.              cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
  63.         student_firstnames.push_back(name);
  64.  
  65.             cout << "Student " << i + 1 <<" score: "; 
  66.             cin >> score;
  67.         cout << endl;
  68.         cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    
  69.         student_score.push_back(score);
  70.  
  71.      i++;
  72.      }
  73.  
  74.      while ( i < numStudents);
  75.  
  76.  
  77.             // sort them alphabetically
  78.         sort (student_lastnames.begin(), student_lastnames.end());
  79.  
  80.     for (int i =0; i < student_lastnames.size(); i++)
  81.     {
  82.             cout << student_lastnames[i] << ", " << student_firstnames[i] << ": " << student_score[i] << endl;
  83.     }
  84.  
  85.  
  86.  
  87. }
  88.  
  89. void students::End()
  90. {
  91.     cout << endl << endl << "Press any key to continue..."; // prompts the user to continue or quit 
  92.     char c = getch();
  93. }
  94.  
  95.  
  96. // function main begins program exectuion
  97. int main() 
  98. {
  99.     students mystudent;
  100.  
  101.     mystudent.displayMessage();
  102.  
  103.     mystudent.getData(); 
  104.  
  105.     mystudent.End();       
  106.  
May 2 '08 #1
5 3741
Laharl
849 Expert 512MB
Use a std::map<string, std::pair<double, string> > (the first string is for the last name, the pair holds two objects (score+first name)) or make a 'student' struct or class. Either will make your life easier. The map stores its keys in sorted order to begin with, and you can overload operators for the struct/class so that they can be easily sorted.
May 2 '08 #2
Use a std::map<string, std::pair<double, string> > (the first string is for the last name, the pair holds two objects (score+first name)) or make a 'student' struct or class. Either will make your life easier. The map stores its keys in sorted order to begin with, and you can overload operators for the struct/class so that they can be easily sorted.

how would I use the map and pair in my code. I have never heard fo them. sorry if it is dumb question, but do i leave it how you wrote it or should i plud in my variable names... also is their a special #include i should use? thanks for the help.
May 2 '08 #3
Laharl
849 Expert 512MB
Go here for a reference on the STL map and pair.
May 2 '08 #4
RRick
463 Expert 256MB
If you are more comfortable with vectors, you can make a structure that contains all your data and then create vector of that structure.
Expand|Select|Wrap|Line Numbers
  1. struct Stuff { string firstName, lastName, etc };
  2. vector<Stuff> vecStuff;
To sort the vector you'll have to use the sort function in algorithm.h. The sort function takes 3 parameters: start iterator, end iterator, and a function that returns a bool for when one Stuff is less than another. I'll let you write the function, but the call looks like:
Expand|Select|Wrap|Line Numbers
  1. bool stuffA_lt_stuffB( const Stuff & a, const Stuff & b)
  2. { you get to define this }
  3.  
  4. std::sort( vecStuff.begin(), vectStuff.end(), stuffA_lt_stuffB);
  5.  
The trick here is that sort uses your 'less than' function to sort the objects. If you want to reverse the sort, make a 'greater than or equal to' function.
May 3 '08 #5
primeSo
35
Just my personal opinion.

I think you have some miss understanding with what your code is doing compare to what you actually wanted it to do.

First of all, you have wroteyou have 3 vectors which store first name, last name and score respectively. Then you wanted them to be sorted by last name.
After your sorting operation, what is sorted is only the vector storing the last name, the other two still remain the same order as previous. Thus, your code certainly not work as you expected.

My suggestion is, redesign.

For example:
Expand|Select|Wrap|Line Numbers
  1. class Student{
  2.    private:
  3.       string lastName;
  4.       string firstName;
  5.       double score;
  6. };
  7.  
  8. int main(){
  9.    vector<Student> studentList;
  10.  
  11.    //treat the 3 variables as a whole.
  12.    Student std1;
  13.  
  14.    std1.lastName = "Prime";
  15.    std1.firstName = "Optimus";
  16.    std1.score = 100.0;
  17.  
  18.    studentList.push_back(std1);
  19.  
  20.    //after you add several Student class object to the vector, then only sort them
  21.   // by last name.
  22.  
  23.   return 0;
  24. }
  25.  
May 13 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
2
by: William Payne | last post by:
Hello, I have two structs: struct FileEntry { FileEntry(const char* name, const char* type, std::size_t file_size, HICON icon) : m_file_size(file_size),
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
0
by: Brian Henry | last post by:
Here is another virtual mode example for the .NET 2.0 framework while working with the list view. Since you can not access the items collection of the list view you need to do sorting another...
6
by: Dennis Gearon | last post by:
This is what has to be eventually done:(as sybase, and probably others do it) http://www.ianywhere.com/whitepapers/unicode.html I'm not sure how that will affect LIKE and REGEX. ...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
13
by: JoeC | last post by:
I am completly lost. I would like to create a function that takes two vectors. These two vectors have objects with x and y coords, what I want to do find all the objects in the same x and y...
14
by: Jinsu Jais | last post by:
Please advice me a program which sorts strings alphebetically. Its very urgent.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.