As the title says, what is the differences between vectors, sets, and tuples in programming?
+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
2009-06-09 23:02:26
good shout on the type distinction!
Beau Martínez
2009-06-09 23:07:06
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
2009-06-09 23:08:01
@Brabster: std::vector in C++ is an example.
RichieHindle
2009-06-09 23:13:42
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
2009-06-09 23:17:14
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
2009-06-09 23:23:57
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
2009-12-10 04:56:32
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
2009-06-09 23:02:48
+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
2009-06-09 23:06:18
double would be the 2 element tuple (2-tuple), although that term (usually) has a different meaning in the context of computers.
TokenMacGuy
2009-06-09 23:09:55
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
2009-06-09 23:11:13
Token: thanks for the list/vector duality, I've clarified that in C++ the vector is an additional type.
John Millikin
2009-06-09 23:16:18