0
Best data structure for customer data.
Inserting /removing data and searching for customer data in C++
3 Respostas
+ 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.
+ 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
+ 1
How about a vector?
https://www.sololearn.com/learn/261/?ref=app