Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <map>
- #include <string>
- #include <algorithm>
- struct Salary
- {
- double Value;
- };
- using Name = std::string;
- // You may want to have this as a shared_ptr.
- using NameSalarySlotMap = std::map<Name, Salary*>;
- using Salaries = std::vector<Salary*>;
- NameSalarySlotMap nameSalaryMap;
- Salaries salaries;
- void add_new_name_and_salary(const char *name, double value)
- {
- Salary *salary = new Salary;
- salary->Value = value;
- // Insert the pair inside the map so we can get a person's salary without iterating the vector.
- // TODO: make sure name is unique and not already present inside the map.
- nameSalaryMap.insert({name, salary});
- // Keep the vector sorted.
- // TODO: it will probably be faster to insert salary directly at the right position instead of sorting the array
- // each time.
- salaries.push_back(salary);
- std::sort(salaries.begin(), salaries.end(), std::greater<>());
- }
- void change_salary_for_name(const char *name, double delta)
- {
- nameSalaryMap[name]->Value += delta;
- std::sort(salaries.begin(), salaries.end(), std::greater<>());
- }
- int main()
- {
- add_new_name_and_salary("Foo", 10);
- add_new_name_and_salary("Bar", 20);
- add_new_name_and_salary("Baz", 15);
- // Will print: 20 15 10
- for (const auto &salary : salaries)
- {
- std::cout << salary->Value << " ";
- }
- std::cout << "\n";
- change_salary_for_name("Baz", 30);
- // Will print: 45 20 10
- for (const auto &salary : salaries)
- {
- std::cout << salary->Value << " ";
- }
- std::cout << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement