views:

1603

answers:

4

As the title says, what is the differences between vectors, sets, and tuples in programming?

+6  A: 

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.

Wikipedia is your friend for all of these.

Brabster
+5  A: 
  • Vector: Ordered collection of objects of the same type.
  • Set: Unordered collection of objects, possibly of the same type or possibly different depending on the collection type and language. Any given object can only appear once.
  • Tuple: Ordered collection of objects of different types.
RichieHindle
good shout on the type distinction!
Beau Martínez
I haven't seen a definition of a vector before that restricts to a single type... interested, can you point me at a resource?
Brabster
@Brabster: std::vector in C++ is an example.
RichieHindle
Oh, I see thanks. Possibly some languages do constrain type for their Vector implementation and others do not (example Java, can take any Object in it's Vector implementation). I hadn't noticed that before.
Brabster
I think that since these terms cross the line into the proof-filled math side of CS, the definition is not necessarily going to be what your co-workers are using the word for.
ryansstack
I disagree with Brabster's assertion that Java doesn't constrain types on Vectors. std::vector also doesn't constrain types either from that thinking, since you can always have a vector of unconstrained pointers. In Java, a Vector contains Object Types. It is not unconstrained.
Ankur Goel
A: 

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.

Matthew Flaschen
+1  A: 

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.

John Millikin
double would be the 2 element tuple (2-tuple), although that term (usually) has a different meaning in the context of computers.
TokenMacGuy
list and vector are distinct concepts in C++. Although they both represent ordered, homogeneous collections, list implies O(1) inserts and vector implies O(1) seek, that is list is implemented as some sort of linked list structure, and vector is implemented as an array.
TokenMacGuy
Token: thanks for the list/vector duality, I've clarified that in C++ the vector is an additional type.
John Millikin