STL container with more than 1 sorting method in C ++

I am looking for a container to contain objects such as Employee (with information: name, salary, phone ....) that can be sorted by name (a..z) and at other times, for example, by salary. What is the best way to do this? I thought about the map, but then I define only 1 key to evaluate each idea (not too advanced, please!)

--- update ---

I really don’t really need to always support 2 STL containers, I usually have 1 (say employees sorted by last name), and on request I don’t mind creating a new STL container and pushing all the elements to it again, only this time to sort salary, so I can print it for this order. Is it possible to create map1 with name sorting and map2 with salary sorting? if I would have liked the further explanation \ example for the definition of these two cards. I have very little C ++ knowledge (the first assignment I received)

+5
source share
5 answers

using this version of std :: sort

template <class RandomAccessIterator, class Compare>
void sort( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

You can sort all the fields you want by supplying your own comparator. For instance,

struct CompareSalary
{
  bool operator () ( const Employee& a, const Employee& b ) const
  {
    return a.salary < b.salary;
  }
}

, std:: sort , , std::vector .

+17

, , Boost MultiIndex

Ps: , ++, Boost MultiIndex.

+8

std::sort:

bool byName(Employee left, Employee right) {
    return left.name < right.name;
}

std::vector<Employee> employees;
std::sort(employees.begin(), employees.end(), byName);
+6

, , . . , .

std:: sort , .

0

, ? std:: map? ( , *(coll.begin())?). , , std:: map, shared_ptr<T>, , . , "" , O (log n) (std:: map - ), .

You will need to synchronize the addition and deletion to make sure that they are added and removed from both cards.

0
source

All Articles