I'm developing a super-small package manager for custom purposes with a simple dependency checker, for that i need to compare two vectors (but same data type, an struct) the first vector has the installed packages by the user (using a simple SQLite query) , and the second vector the required packages to install.
The struct code is like:
typedef struct {
std::string UniformTypeID;
std::string PackageName;
unsigned int Version;
} PackageInfo;
i need to know how to compare these two vectors but here's a problem, the size of the two vectors may vary, for example, if i use the size of the installed packages vector is smaller than the dependency packages (more larger), no find any package in their limit, but the requested package is more beyond of their limit.
I can use C++11.
Provide a strict weak ordering predicate for your PackageInfo
class, either in terms of a binary predicate or simply provide an operator<
.
With that you can sort your arrays (installed packages and required packages). Then you can use the set manipulation functions of the STL
:
std::vector<PackageInfo> installed_packages = ...;
std::vector<PackageInfo> required_packages = ...;
std::vector<PackageInfo> met_requirements;
std::sort(installed_packages.begin(), installed_packages.end());
std::sort(required_packages.begin(), required_packages.end());
std::set_intersection(
installed_packages.begin(), installed_packages.end(),
required_packages.begin(), installed_packages.end(),
std::back_inserter(met_requirements));
met_requirements
is now the set of PackageInfo
that is present in both the installed_pacakges
and required_packages
. You can then verify if all of the necessary required_packages
are available.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments