Differences between vector, set, and tuple

后端 未结 7 503
庸人自扰
庸人自扰 2021-01-30 17:15

What are the differences between vectors, sets, and tuples in programming?

相关标签:
7条回答
  • 2021-01-30 17:29

    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.

    0 讨论(0)
  • 2021-01-30 17:32

    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.

    0 讨论(0)
  • 2021-01-30 17:38

    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.

    0 讨论(0)
  • 2021-01-30 17:40

    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.

    0 讨论(0)
  • 2021-01-30 17:41

    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 this 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 who rows are called in Relational Algebra.

    • A Vector (terrible name by the way) 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.

    0 讨论(0)
  • 2021-01-30 17:44
    • 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.
    0 讨论(0)
提交回复
热议问题