The template tools are divided into three areas: Containers, Algorithms, and Iterators.
Containers hold your data, Iterators access your data, and Algorithms operate on your date.
(Algorithms use Iterators to access the data held in the Containers)
This is what Brother Bob is talking about when he said:
Make a structure that contains all the information you need.
struct person {
int number;
int pancakes;
};
Sort an array of persons based on the pancakes-field.
The container you want to use, is probably std::vector. std::vector is a array that can be resized easily, and is the default "go to" container you mostly want to use, unless you are trying to do something specific, and until you know the pros and cons of the other containers. (Each container, including std::vector, has tradeoffs - these tradeoffs are important to learn, but when in doubt, std::vector is usually a safe bet).
The iterator being used in this case is happening behind the scenes, so you don't need to worry about it. But if you're curious, it's a random-access iterator. (You'll learn more about these in the future, so don't worry if you don't understand it right now - I didn't bothered looking at them for years, and the fine details of how iterators work internally can be mostly ignored until later. Containers are more important, and Algorithms also. Understanding iterators helps you understand how Algorithms and Containers work, but aren't required knowledge to use them).
The algorithm you want to use in this case is std::sort. std::sort takes a range of elements* in a container, and sorts them in order from least to greatest. Ofcourse, what's "least" or "greatest" depends on the type of data given... data is expected to be able to provide their own comparison functions, or else a comparison function has to be passed to std::sort. Ints have built-in comparison functions (< > != ==, etc...).
*An 'element' is a fancy name that means "a piece of data a container holds". Like an array, myVector[0] accesses the first element in the container.
So, using std::vector to hold our pancake-eating heroes, and using std::sort to sort them by the number of pancakes eaten, we get this code:
struct Person
{
int id; //The number used to identify the person.
int pancakes; //The number of pancakes eaten.
};
bool PersonPancakeCompare(const Person &personA, const Person &personB)
{
//If personA is less than personB, return true.
//Here, we're measureing by number of pancakes eaten.
return (personA.pancakes < personB.pancakes);
}
int main()
{
std::cout << "How many people are eating pancakes?\n"
<< "Num people: ";
int numPeople = 0;
std::cin >> numPeople;
//A std::vector container of the 'Person' struct.
//The vector is currently empty (we haven't added anyone yet).
std::vector<Person> people;
for(int i = 0; i < numPeople; i++)
{
Person person;
person.id = i; //Keep track of what number this person is.
std::cout << "How many pancakes did person " << (i+1) << " eat?\n"
<< "Pancake count: ";
std::cin >> person.pancakes;
//Add the person to the end of the std::vector.
//push = "add to", back = "the back of the vector".
//push_back(person) = "add to the back of the vector, the data in 'person'".
people.push_back(person);
}
//Use std::sort algorithm on the std::vector, from the vector's begin()ing, to the vector's end().
//Since the struct "Person" is a new type of struct, we need to tell std::sort what makes a 'Person' less-than or greater-than
//another Person. So we provide our own comparison function, "PersonPancakeCompare()" to tell std::sort() how to compare 'Person' variables.
std::sort(people.begin(), people.end(), PersonPancakeCompare);
//Now, we can print out the results.
std::cout << "\n\n-------------------------\n"
<< "Results:" << std::endl;
//Loop over the entire (already-sorted) vector, and print out each person.
for(int i = 0; i < people.size(); i++)
{
Person &person = people;
std::cout << "Person " << (person.id + 1) << " ate " << person.pancakes << " pancake(s)!" << std::endl;
}
return 0;
}
std::sort() isn't a bubble sort, though, because in this situation a bubble sort isn't needed. If however, this is a homework question, and the teacher specified "use a bubble sort", then this code doesn't qualify and would fail the teacher's requirement. But ofcourse this isn't a homework question, is it? Because if it is, you conveniently forgot to mention it in your original post.