When working with collections containing pointers to objects, handling projections requires a slightly different approach. Instead of accessing the object directly, you need to dereference the pointer to access the object it points to.
Consider a collection of Player
pointers that you want to sort based on their Level
attribute using a projection function. Here's an example:
#include <vector>
#include <iostream>
#include <algorithm>
#include <memory>
struct Player {
std::string Name;
int Level;
};
int main() {
std::vector<std::shared_ptr<Player>> Party {
std::make_shared<Player>(Player{"Legolas", 49}),
std::make_shared<Player>(Player{"Gimli", 47}),
std::make_shared<Player>(Player{"Gandalf", 53})
};
std::ranges::sort(Party, {},
[](const std::shared_ptr<Player>& P) {
return P->Level;
});
for (const auto& P : Party) {
std::cout << "[" << P->Level << "] "
<< P->Name << "\n";
}
}
[47] Gimli
[49] Legolas
[53] Gandalf
In this example, the projection function is a lambda that takes a shared_ptr<Player>
and returns the Level
of the Player
object by dereferencing the pointer (P->Level
). This approach ensures that the algorithm works correctly with pointers to objects.
->
operator to access members of the object pointed to by the pointer.shared_ptr
, unique_ptr
, raw pointers, etc.).By understanding how to handle pointers within projection functions, you can effectively use projections with collections containing pointers, enabling more flexible and powerful data manipulation.
Answers to questions are automatically generated and may not have been reviewed.
Learn how to use projection functions to apply range-based algorithms on derived data