Improve Map access

This commit is contained in:
Matti 2024-11-23 08:12:49 +01:00
parent dbdd4ade8e
commit 9321ec8d71

View File

@ -45,25 +45,14 @@ void deleteAllPointers(std::vector<ProductSale*> &allSales) {
void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
for (ProductSale* singelSale : allSales) {
// map.add(singleSale)
if (singelSale->country != country) {
// Filter out wrong countries
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->itemType) {
element.second++;
alreadyExists = true;
break;
TypeCounter[singleSale->itemType]++;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->itemType] = 1;
}
}
// return map.getMostCommon
std::string mostPopularType;
@ -83,29 +72,18 @@ void getMostPopularTypeIn(std::string country, std::vector<ProductSale*> &allSal
void getOnlineVsOfflineIn(std::string country, std::vector<ProductSale*> &allSales) {
std::map<std::string, int>TypeCounter;
for (ProductSale* singelSale : allSales) {
// map.add(singleSale)
if (singelSale->country != country) {
for (ProductSale* singleSale : allSales) {
if (singleSale->country != country) {
continue;
}
bool alreadyExists = false;
for (auto element : TypeCounter) {
if (element.first == singelSale->salesChannel) {
element.second++;
alreadyExists = true;
break;
// Increment the counter for the sales channel directly
TypeCounter[singleSale->salesChannel]++;
}
}
if (!alreadyExists) {
TypeCounter[singelSale->salesChannel] = 1;
}
}
int OnlineCounter = TypeCounter["Online"];
int OfflineCounter = TypeCounter["Offline"];
double ratio = (OfflineCounter == 0) ? 100.0 : static_cast<double>(OnlineCounter) / OfflineCounter * 100.0;
double ratio = (OfflineCounter == 0) ? 100.0 : static_cast<double>(OnlineCounter) / OfflineCounter * 50.0;
std::cout << "Online : " << OnlineCounter << std::endl << "Offline : " << OfflineCounter << std::endl;
std::cout << "Ratio : " << ratio << "% Online" << std::endl << std::endl;