0

Best data structure for customer data.

Inserting /removing data and searching for customer data in C++

17th Aug 2021, 5:33 AM
Yusmel Pradera
Yusmel Pradera - avatar
3 Antworten
+ 2
std::vector is ideal. List is fast in inserting and removing. The time complexity is O(1), but it can't random access, so you need a workaround for searching. vector on the other hand allows random access with O(1); inserting and removing are O(n) except push_back and pop_back is O(1). Because it gives all you need initially, it's easier to use.
17th Aug 2021, 5:58 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
For smaller amounts of data, you could probably get away with using std::vector<> or std::list<> and running linear searches. However, for larger sets of data, linear searches will start to hurt, because you will likely run searches not only when actually searching for an entry, but also when inserting and removing entries (to confirm whether the customer to be entered/removed exists). In such cases, maps are very useful, but which kind of map depends on what your customer class encapsulates, and whether the structure needs to be ordered: std::map<> -> entries are ordered by key, only one entry per key std::multimap<> -> entries are ordered by key, multiple entries per key std::unordered_map<> -> unordered entries, only one entry per key std::unordered_multimap<> -> unordered entries, multiple entries per key It makes sense to carefully think about what property to use as a key, and the operations you want to support. As an example, I threw this together: https://code.sololearn.com/c98eVnOHwV5q
17th Aug 2021, 12:07 PM
Shadow
Shadow - avatar
17th Aug 2021, 5:57 AM
Ipang