Javatpoint Logo
Javatpoint Logo

Difference between std::set vs std::vector in C++

In this article, we will discuss the difference between std::set and std::vector in C++. But before discussing the differences, we must know about the std::set and std::vector in C++.

What is the std::vector?

A vector is a dynamic array-like container in C++ that may hold many elements of the same data type. Vectors, which are unlike arrays, can increase and shrink in size during execution. Vectors are provided in the <vector> header file as part of the C++ Standard Template Library (STL).

Example:

Uses of Vector in C++:

Vectors can be used in the following ways in C++:

Collections: Vectors can hold collections of items of the same data type, such as integers, numbers with floating points, or messages.

Size: Vectors can expand and decrease in size during runtime, making them handy when we don't know the size of the collection in advance of time or when the size of the collections may vary during runtime.

Fast random access: They can be accessed in constant time using their index because items in a vector are stored in memory with adjacent regions.

Efficient memory allocation: Vectors allocate memory effectively, allowing us to add or delete members from the vector's end with minimum cost.

Interoperability with various other libraries: As vectors are part of the C++ Standard Template Library, vectors can be used with various STL containers, computations, and iterators.

What is std::set?

Sets are a type of associative container in which each element must be unique since the value of the component identifies it. The data are saved in a certain sorted order, either increasing or decreasing. The std::set class is part of the C++ Standard Template Library (STL), which is defined within the <set> standard header file.

Some of the most popular set attributes in C++ are as follows:

The attribute of Uniqueness: In C++, each component of a set must be unique, which means no duplicate values are permitted. As a result, sets in C++ do not promote redundancy.

Sortedness: The components of a set container are stored by default in a sorted way.

Immutability: Once values are saved in a set, they cannot be changed. As a result, insertion and deletion are permitted, but we cannot update or edit the set's current items.

Internal Implementation: In C++, the logical construction of a set is done via a BST.

Differences between std::set and std::vector:

Difference between std::set vs std::vector in C++

There are several differences between std::set and std::vector. Some main differences of the std::set and std::vector are as follows:

Features std::set std::vector
Fundamental Data Structure Balanced Binary Search Tree (for instance, Red-Black) The dynamic Array (Contingent Memory Block)
Iteration over Elements Sequential repetition based on insert order. Ordered repetition based on sorting.
Memory Management It allocates resources for each element dynamically. It allocates contiguous blocks for all items.
Sorting Customizing The comparison operators are used to order the elements. Algorithms can be used to customize ordering.
Insertion Effectiveness It is slower because of balancing operations. It is faster, especially for insertions at the conclusion.
Reallocation of Memory There is no requirement for reallocation during insertions. Insertions could require reconfiguration.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA