Sorting a vector list (need idea)

Jul 7, 2014 at 12:59am
hi, i need some ideas to do the sorting for my code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void displaymanager(){
    system("cls");
    cout << "\t===============================================================" << endl;
    cout << "\t|             BOGS BONNY VEHICLE MANAGEMENT SYSTEM            |" << endl;
    cout << "\t===============================================================" << endl;
    cout << endl;
    for (int i=0; i<vehicleCount; i++){
        cout << "\t" << Managers[i].getName() << endl;
        for (int n=0; n<carCount; n++){
            if(Cars[n].getManager() == Managers[i].getName())
                cout << "\t" << Cars[n].getModel() << endl;
        }
        for (int n=0; n<busCount; n++){
            if(Buses[n].getManager() == Managers[i].getName())
                cout << "\t" << Buses[n].getModel() << endl;
        }
        for (int n=0; n<airplaneCount; n++){
            if(Airplanes[n].getManager() == Managers[i].getName())
                cout << "\t" << Airplanes[n].getModel() << endl;
        }
        cout << endl;
    }
    cout << "\t===============================================================" << endl;
}


currently the output is like this

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

Alex
Car3
Bus2
Airplane1

James
Car1
Car2
Bus1

Murphy
Airplane2
Airplane 3

Murphy
Airplane2
Airplane 3


As you can see, there are repetitive and redundant data on the display. I want it to look just like this

1
2
3
4
5
6
7
8
9
10
11
12
13
James
Car1
Car2
Bus1

Alex
Car3
Bus2
Airplane1

Murphy
Airplane2
Airplane 3


Im kinda sure for loop and if-else are needed to solve this. But i'm not sure what to code though. Can anyone give some ideas. Thanks
Jul 7, 2014 at 1:05am

Im not sure if i did understand, but you can try this:

Use vector<type> name
from #include <vector>
and std::sort(firstElement, lastElement)
from #include <algorithm>

http://www.cplusplus.com/reference/algorithm/sort/
Last edited on Jul 7, 2014 at 1:07am
Jul 7, 2014 at 1:09am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
but won't the output be like

James
Car1
Car2
Bus1

James
Car1
Car2
Bus1

Alex
Car3
Bus2
Airplane1

Murphy
Airplane2
Airplane 3

Murphy
Airplane2
Airplane 3 


I dont want any repititive data. what is the condition do i need to write in the for loop or if-else statement?
Jul 7, 2014 at 1:12am
As soon as you have it sorted(std::sort()), it will look like:

1
2
3
4
5
6
7
8
9
10
11
Alex
Alex
Bus1
Bus1
...
...
James
James
...
...


so its easier to remove the repeated ones from that sorted vector.
Last edited on Jul 7, 2014 at 1:14am
Topic archived. No new replies allowed.