Reputation: 2387
I am little confused about the difference between containers and collections. I read about them on Wikipedia and this is what I have understood..
Collections : Stores variable number of objects. Ex. list, set...
Containers : DataStructures? Ex. dequeue, stack..
But I thought collections like list, tree etc are data structures, then what are containers. Also what does this mean..
Data structures that implement containers include arrays, lists, maps, queues, sets, stacks, tables, trees, and vectors.
Upvotes: 6
Views: 10028
Reputation: 20739
The difference is more of taste than substance, but the feeling I have when listening the two terminologies is that
This is clearly subjective, but considering how C++ container are implemented (value semantics) and how Java colloection behave (polymorphism of the objects), and what Java variable technically are (autodereferenced pointers) it is probably all due to the value/pointer semantics.
A C++ container cannot be polymorphic in values, but can be homomorphic in pointers to polymorphic values, so a C++ collection<T>
can just be a container<unique_ptr<T> >
(choose the container you want to concretize), where container<T>
has no strict java correspondence.
Upvotes: 3
Reputation: 6797
The ISO C++ standard really does not establish a very clear distinction. It does provide a formal definition for container (23.2.1):
Containers are objects that store other objects.
... yet it provides no such formal definition for collection and uses that term a bit more generally. It describes tuple, for example, as a collection of objects, even though by the very definition above, it is also a container.
Collections : Stores variable number of objects. Ex. list, set...
Containers : DataStructures? Ex. dequeue, stack..
But I thought collections like list, tree etc are data structures, then what are containers. Also what does this mean..
As far as I see it, and the standard does not specify anything to the contrary, these are both collections and containers. They also happen to be data structures, but 'data structure' is a very general computer science term for any kind of aggregate which organizes data in some way.
With C++, where you'll find a more useful distinction is in concepts that the original STL, as conceived by Stephanov, defined.
A Sequence is a variable-sized Container whose elements are arranged in a strict linear order. It supports insertion and removal of elements.
The above describes containers like list
, deque
, and vector
.
A Sorted Associative Container is a type of Associative Container. Sorted Associative Containers use an ordering relation on their keys; two keys are considered to be equivalent if neither one is less than the other. (If the ordering relation is case-insensitive string comparison, for example, then the keys "abcde" and "aBcDe" are equivalent.)
The above describes containers like set
and map
.
A Pair Associative Container is an Associative Container that associates a key with some other object. The value type of a Pair Associative Container is pair. [1]
The above describes containers like map
.
As to further reading on this subject with respects to the slight semantic differences: OOP Terminology: "Container" & "Collection"
Upvotes: 6
Reputation: 198171
Java doesn't really use the term "container," except in the context of AWT GUI components, in which a Container
represents a GUI component that can contain other components.
Collection
is used in Java for data structures that contain objects, as you describe.
Computer science in general tends to treat them as more or less synonymous.
Upvotes: 11
Reputation: 6890
" The C++ language standard (ISO/IEC 14882-1998[E]) says in Clause 23 that containers are "components that C++ programs may use to organize collections of information," and adds: "Containers are objects that store other objects. They control allocation and deallocation of these objects through constructors, destructors, insert and erase operations." It goes on to list the two general types of containers: sequences and associative containers.
The C++ standard says, "A sequence is a kind of container that organizes a finite set of objects, all of the same type, into a strictly linear arrangement." The three types of sequences are vector, list, and deque.
C++ Containers Cannot Contain Mixed Types!
Perhaps the most useful C# collection isn't considered a collection at all. C# arrays are much more powerful than C++ arrays, and they natively support many operations that require a class in C++. For instance, the Length property tells you the number of elements in the array, like the size() method in C++ containers. "
take from http://soa.sys-con.com/node/39460, you can read this article for good understand of Container and Collection concepts.
Upvotes: 9