Note that there are some explanatory texts on larger screens.

plurals
  1. POMaintaining a unique set of elements on different criteria C++ STL
    primarykey
    data
    text
    <blockquote> <p>I have to develop a component which will have a more than 100,000 instances of a class. And i want to generate a report based on the different different criteria (members) of the particular class. for example, A employee class with data fields id, names, addr, phoneno. Report generation wiil be based on</p> </blockquote> <hr> <ol> <li>names_ascending</li> <li>names_descending</li> <li>addr_ascending</li> <li>phoneno_asceding</li> <li>unique_names</li> <li>unique_addr</li> <li>unique_phoneno</li> </ol> <p>runtime iteration of instances for each call is very slow since it is a linear operation on large number of instances and requires sorting mechanism.</p> <p>So i have stored a pointers of each instances in a container on different sorted manner. But requires more memory than required. Please suggest me a better way of doing this. I have posted sample code snippet that i followed to achieve above.</p> <pre><code>class Employee { int m_id; string m_name; string m_addr; string m_phone; public: Employee(int id, string name, string addr, string phone) : m_id(id), m_name(name), m_addr(addr), m_phone(phone) { } int id() const { return m_id; } string name() const { return m_name; } string addr() const { return m_addr; } string phoneno() const { return m_phone; } }; //custom predicate for std containers struct IDComparator { bool operator() (const Employee* e1, const Employee* e2 ) { return e1-&gt;id() &lt; e2-&gt;id(); } }; struct NameComparator { bool operator() (const Employee* e1, const Employee* e2 ) { return e1-&gt;name() &lt; e2-&gt;name(); } } struct AddressComparator { bool operator() (const Employee* e1, const Employee* e2 ) { return e1-&gt;addr() &lt; e2-&gt;addr(); } }; struct PhoneComparator { bool operator() (const Employee* e1, const Employee* e2 ) { return e1-&gt;phoneno() &lt; e2-&gt;phoneno(); } }; //Class which holds huge number of employee instances class Dept { private: typedef set&lt;Employee*, IDComparator&gt; EMPID; //unnique id typedef EMPID::iterator EMPID_ITER; typedef multiset&lt;const Employee*, NameComparator&gt; EMPNAME; // for sorted names typedef EMPNAME::iterator NAME_ITER; typedef multiset&lt;const Employee*, AddressComparator&gt; EMPADDR; // for sorted addr typedef EMPADDR::iterator ADDR_ITER; typedef multiset&lt;const Employee*, PhoneComparator&gt; EMPPHONE; // for sorted phoneno typedef EMPPHONE::iterator PHONE_ITER; private: EMPID m_empids; EMPNAME m_names ; EMPADDR m_addr; EMPPHONE m_phoneno; public: Dept() { } ~Dept() { //delete the instances of employees } void add(Employee* e) { EMP_ITER iter = m_empids.insert(e).first; const Employee* empptr = &amp;*iter; m_names.insert(empptr); // adds employee pointer to name multimap m_addr.insert(empptr); // adds employee pointer to addr multimap m_phoneno.insert(empptr); // adds employee pointer to phone multimap } void print_emp_dtls() const; //prints all the emp dtls iterating though EMPID void print_unique_names() const; //iterate EMPNAME &amp; use upperbound &amp; lowerbound, prints unique names void print_asc_name() const; //iterate EMPNAME &amp; prints all names in ascending order void print_desc_name() const; //back iterate EMPNAME &amp; prints all names in descending order void print_unique_adrr() const; //iterate EMPADDR &amp; use upperbound &amp; lowerbound, prints unique address void print_asc_addr() const; //iterate EMPADDR &amp; prints all addr in ascending order void print_desc_addr() const; //back iterate EMPADDR &amp; prints all address in descending order void print_unique_phoneno() const; //iterate EMPPHONE &amp; use upperbound &amp; lowerbound,prints unique phoneno void print_asc_phoneno() const; //iterate EMPPHONE &amp; prints all phoneno in ascending order void print_desc_phoneno() const; //back iterate EMPPHONE &amp; prints all phoneno in }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload