Reputation: 39437
What are the differences between vectors, sets, and tuples in programming?
Upvotes: 39
Views: 33474
Reputation: 313
the difference is tuples are not meant to be treated as collections, while vectors & sets are.
tuples are meant to represent compound values, like position in a 3d space with (x,y,z) coordinates, it doesn't make sense to see it as a collection, because yes is a list of 3 numbers but the 3 numbers have different meaning and combined form an specific meaning.
Think of tuples as a structs with nameless positional attributes that can have different types. As such is not meant to store lots of values, and this is why all implementations of these things are optimized for a small number of them
an example of tuple is the type used to describe the columns of a single row in a SQL Database, in fact that is what rows are called in Relational Algebra.
A Vector (terrible name by the way, because the word in math is used for compound values not for collections) is an ordered collection that is meant to store lots of values, it is usually implemented to grow as needed keeping an access of O(1)
a table in a SQL DB without primary key would be a Vector of tuples.
A Set is a collection of unique elements, that can be ordered but it doesn't have to be.
a table in a SQL DB with primary key would be Set of tuples.
Upvotes: 2
Reputation: 5095
Mathematically
A tuple has properties that distinguish it from a set.
- A tuple may contain multiple instances of the same element, so tuple (1,2,2,3) != (1,2,3) but set {1,2,2,3} = {1,2,3}.
- Tuple elements are ordered: tuple (1,2,3) != (3,2,1), but set {1,2,3} = {3,2,1}.
- A tuple has a finite number of elements, while a set or a multiset may have an infinite number of elements.
Vector is a different type represented by multiple tuples.
Cheers :-)
Upvotes: 8
Reputation: 43560
A vector
is an ordered sequence of items that does allow duplicates.
A set
is a collection of items that is unordered and does not allow duplicates.
A tuple
is an ordered sequence of items of a given length.
Upvotes: 21
Reputation: 545
Vectors have an ordering
Tuples are ordered and can have repeat elements.
Sets are unordered and repeat elements do not change the set.
For example: {a,b}, {b,a}, and {b,b,a} are all the same set, while (a,b), (b,a) and (b,b,a) are all different tuples.
Upvotes: 3
Reputation: 200826
A tuple is a heterogeneous collection of objects, which should be treated as a single unit: for example, ("John", "Smith", 30) is a (String, String, Integer) tuple.
A list (in C++: and also vector) is a homogeneous collection of objects -- that is, each object can be treated uniformly. Whether they are actually the same type depends on the language, but the point is that they can be processed the same way.
A set is an unordered unique homogenous collection -- you know what objects it contains, and what type they are, but not in what order, and it only contains one of each object.
Upvotes: 6
Reputation: 284836
Vectors have an ordering, sets do not (and can't have duplicates), and tuples are close to vectors but are usually used more like structs in practice.
Upvotes: 0
Reputation: 281505
Upvotes: 82