A packing list has several useful views. You may want weights in ascending order, the number of heavy parcels, and a sorted list of distinct destinations. These are related questions, but each calls for a different tool. A vector preserves all weight entries, count_if counts values satisfying a rule, and a set removes repeated destination names.
This practical keeps those responsibilities in separate helpers. The sorting helper receives its vector by value so it can reorder a copy and return that copy. The counting helper receives a const reference because it only reads. The destination helper returns a new set built from the supplied name sequence. Choosing parameters this way makes the intended mutations visible.
The starter includes the same smallerFirst comparison used in the sorting lesson. Pass its name as the third argument to std::sort; it compares two weights using <.
Use the full half-open range whenever an algorithm should examine every element. The last element still matters even though the end iterator itself is never dereferenced. For the heavy-parcel rule, include the stated boundary weight. Equal weights remain separate parcels; only duplicate destination names are removed.
#include <iostream>
#include <set>
#include <string>
#include <vector>
std::set<std::string> uniqueRooms(const std::vector<std::string>& names) {
std::set<std::string> rooms;
for (std::string name : names) { rooms.insert(name); }
return rooms;
}
int main() {
for (std::string room : uniqueRooms({"Studio", "Office", "Studio"})) {
std::cout << room << '\n';
}
}
The example prints Office and Studio. The braced argument creates a vector for the function to read; the returned set keeps the distinct room names. In your packing report, work through the helpers separately. ordered changes its local copy, heavyCount only reads the weights, and destinations builds a new set. The output calls are already complete. Keep them unchanged so each line shows the result of its helper.
ordered() with std::sort, passing the supplied smallerFirst function as its third argument. The first probe should print Weights: 2 2 5 7.heavyCount() using std::count_if and a lambda that accepts weights of at least 5 kg. Convert the small count to int; the probe should print Heavy parcels: 2.name into result inside destinations(). The final probe should print Destinations: East West.Edit main.cpp, then press Run to inspect the Console. Press Submit to check your work. Keep the supplied probes in place.