0
Can anybody tell me about containers ?
3 Answers
0
containers are utility class providing standard data structure set and iterator for simple operation with data
0
Containers are data structures that are designed to hold multiple data elements.
Data structures, as the name suggests, are a means to hold data in a structured, i.e. ordered way. A data element can be anything from an int, an object, a pointer or another container (so containers can hold containers).
In real-life we use containers to remember / keep items of physical or non-physical nature (bags, or shopping list, schedule) in an easily accessible place.
In programming, as in real-life we use containers to remember / keep items in an easily accessible place, although in programming we only keep non-physical items (you can't store a banana in a C++ container but you could write a banana class(non-physical item), create an instance of it and store it in a C++ container :-) ).
Containers are useful in almost all non-trivial programs. As algorithms in programs have different needs on containers there is a sizeable number of containers and their implementation.
The most commonly used containers are arrays, vectors, lists, sets and maps. All are either directly built-in into C++ (arrays) or provided by the standard library (all others mentioned).
These containers have different properties that help algorithms to be more readable, easier to express and / or faster. They organize their data elements differently to support some basic operation e.g. not at all, slowly or fast.
The most common operations on containers are element insertion and deletion, element search and direct element access.
All mentioned containers from the standard library manage their own size. If you want to use the containers from the standard library, just #include their header <vector>, <list>, <set> or <map>.
If you have any questions or tipps on how to improve the answer pls let me know. :-)
0
Here's an overview about the above mentioned containers:
* arrays:
* no insertion and deletion, as they are of fixed size and only provide direct access to their elements
* direct access: very fast
* search: supported but depends on programmer as arrays do not organize their data elements themselves
* std::vector ("self-managing array")
* insertion: pretty much like arrays *but* is not of fixed size (manages its size on its own) and supports insertion and deletion
* direct access: very fast
* search: same as arrays
* std::list
* insertion and deletion: very fast
* direct access: not available
* search: slow
* std::set
* insertion and deletion: fast (not very) as std::set needs to order all its elements
* direct access: not available
* search: very fast as std::set organizes its data in an internal tree data structure
* specialties: set operations like difference, intersection etc are defined and fast
* std::map (associative container)
* insertion and deletion: fast
* direct access: fast
* search: not so fast :-)
* specialty: std::map is an associative container, you access the values in it by passing identifying value (called "key"). Therefore arrays and vectors are also associative containers as you give them an index and they return the value stored for that index / key.