Differences between Vector and List in C++28 Aug 2024 | 6 min read In this article, you will learn about the difference between Vector and List in C++. But before discussing the differences, you must know about the Vector and List. What is Vector in C++?In C++, a vector is a dynamic array-like container that can store a collection of elements of the same data type. Unlike arrays, vectors can grow or shrink in size during runtime. Vectors are part of the C++ Standard Template Library (STL) and are included in the <vector> header file. Some uses of vectors in C++:
Syntax:The basic syntax of declaring a vector: We can also specify an initial size for the vector by passing an integer argument to the corresponding constructor: What is List in C++?In C++, a list is a container that stores a collection of elements, similar to a vector or an array. However, unlike vectors and arrays, lists are implemented as doubly-linked lists, where each element is linked to both the previous and the next elements in the list. It allows for efficient insertion and deletion of elements at any position in the list but comes at the cost of slower random access to elements compared to vectors or arrays. Lists allocate memory efficiently since each element is stored as a separate node and can be dynamically resized without the need for contiguous memory allocation. Key differences between Vector and ListVector and list are container classes that allow the user to store a collection of elements, and there are some differences between the two, which are as follows:
Example:Basic C++ code snippet that demonstrates the differences between a vector and a list: Output: Time taken by vector for insertion: 33ms Time taken by list for insertion: 131ms Time taken by vector for random access: 0ms Time taken by list for random access: 3163ms Time taken by vector for sorting: 346ms Time taken by list for sorting: 704ms Explanation: This code creates a vector and a list and inserts 1 million elements into each container using push_back(). After that, it randomly accesses 1000 elements in each container and measures the time taken for each operation. Finally, it sorts the elements in the vector using the sort() function and the elements in the list using the sort() member function and measures the time taken for each operation. The results of this code will depend on the specific machine and environment where it is executed, but in general, it should illustrate some of the performance differences between vectors and lists for different types of operations. Next TopicDynamic Cast in C++ |
In this article, we will discuss how to take space-separated input in C++. If we want to take space-separated input in C++, we will use the cin object along with the extraction operator ">>". Program 1: Let us take an example to illustrate how to take the space-separated...
4 min read
In this article, you will learn about the flattening a linked list in C++ with different approaches and examples. Flattening a linked list in C++ means converting a linked list of linked lists into a single, sorted linked list. It is a common problem in data structures...
22 min read
An ordered map in C++ is a container that stores key-value pairs in a sorted order, based on the keys. It is implemented as a balanced binary search tree, which allows for efficient access, insertion, and deletion of elements. To use an ordered map in C++, you...
4 min read
Modular exponentiation is a base fundamental algoritm in number theory and cryptography that works to efficiently find the remainder of an integer raised to a power then divided by another integer. This algorithm proves to be quite productive in cases where working with large numbers...
5 min read
In the world of programming, parameters are an integral part of the method of passing data from one component to another. C++ provides a mechanism for passing data between functions through the use of actual and formal parameters. In this article, we will explore the concepts...
4 min read
In this article, you will learn about the std::regex_iterator in C++ with its syntax and examples. What is the std::regex_iterator? A C++ Standard Library class called std::regex_iterator represents an iterator for paging over a string sequence that matches a regular expression. It is a component of the regular...
3 min read
In combinatorial mathematics and computer science, the Stable Marriage Problem is a well-known Puzzle. It entails establishing a stable match between two sets of elements, such as men and women, in which each has distinct preferences for the individuals who make up the other group. If...
4 min read
Introduction: The default method of interacting with strings in C++ is called std::string since it provides users with a wide range of useful functionalities. Among many other string operations, std::string offers string operations, including looking for substrings, comparing strings, concatenating strings, and slicing strings. But each time...
5 min read
A is a decision taking diagram that follows the sequential order that starts from the root node and ends with the lead node. Here the leaf node represents the output we want to achieve through our decision. It is directly inspired by the binary tree....
3 min read
C++ is a similar kind of programming language which merges the features of the C programming language and Simula67 (it was recognized as the first object Oriented language). C++ set up the concept of Classes and Objects. Are you looking for a good book to start with...
6 min read
We request you to subscribe our newsletter for upcoming updates.
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