Array of Struct sort

This is a discussion on Array of Struct sort within the C++ Programming forums, part of the General Programming Boards category; I have read many topics regarding sort but i wasn`t able to sort them. I am new in C++. What ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    10

    Array of Struct sort

    I have read many topics regarding sort but i wasn`t able to sort them. I am new in C++. What changes i must do to sort the names alphabetically ?

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <string>
    using namespace std;
    
    class classroom
    {
    public:
        string studentName;
    
        void classroom::createStudent(string);
    };
    
    void classroom::createStudent(string name)
    {
        studentName=name;
    }
    
    int main()
    {
        //number of students
        const int numstudents=10;
        
        //create structure
        classroom student[numstudents];
    
        //initialize variables
        student[0].createStudent("Alexa Trina");
        student[1].createStudent("George Ali");
        student[2].createStudent("Comina Riviera");
        student[3].createStudent("Dimitri Askin");
        student[4].createStudent("Erato Georgin");
        student[5].createStudent("Georgina Mare");
        student[6].createStudent("Konan Varvi");
        student[7].createStudent("Lampros Anesti");
        student[8].createStudent("Marina Parate");
        student[9].createStudent("Nikon Smith");
    
        return 0;//indicate that program end succesfully
    }//end main
    Thanks for you time!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,672
    Quote Originally Posted by fukki View Post
    What changes i must do to sort the names alphabetically ?
    1. #include <algorithm>
    2. Define the less-than operator for your classroom class (operator<). Have this operator compare the std::string studentName member variable.
    3. Call the stl std::sort function on your array.


    ...and presto your array is sorted.



    From a design standpoint it does not make a lot of sense to have a classroom class that stores a single student... and then create an array of classroom objects (each storing a single student) to represent one "classroom". It would make more sense to have a student class representing a single student and then a classroom class representing a single classroom (of multiple students). The classroom object could have an internal std::vector<student> container that stores all the students for that class and an AddStudent function that creates a new named student object and inserts that onto the classroom object's internal student vector. But, that's just my opinion.
    I used to be an adventurer like you... then I took an arrow to the knee.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  2. array of struct pointers
    By simo_mon in forum C Programming
    Replies: 4
    Last Post: 05-11-2009, 08:34 PM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21