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:There are several differences between std::set and std::vector. Some main differences of the std::set and std::vector are as follows:
Next Topicforward_list merge() in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India