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++ |
Tutorial Compiler Programs OOPs STL Interview Questions | C++ Programming Examples C++ programs are frequently asked in the interview. These programs can be asked from basics, array, string, pointer, linked list,...
2 min read
Merging overlapping intervals is a common computational problem that arises in various domains, including computer science, mathematics, and real-world applications like scheduling, calendar management, and data analysis. The goal is to take a collection of intervals, where each interval represents a range of values, and merge...
18 min read
Splay Trees is a type of binary search tree. It has a unique feature in which they dynamically alter their structure based on recent access history. This ability makes them particularly efficient for certain operations, and one such operation is the insertion of nodes. In this...
5 min read
Introduction: The maximum width of a binary tree with zeros is a concept that requires finding the maximum number of nodes at any level of the binary tree, including both actual nodes and places where zeros (or empty nodes) can occur. Width is defined as the number...
9 min read
In this article, we will discuss how to count smaller elements on the right side in C++ with several examples. Below is the N-dimensional unsorted array arr[], which is made up of unique integers. Our objective is to create a second array count where the count will...
9 min read
In C++, an enormous number of pre-built functions and libraries are available for handling strings. Strpbrk() is one of the less well-known but very helpful routines. This function is a component of the <cstring> header and is part of the C Standard Library. Its main purpose...
4 min read
Dangling pointer A dangling pointer is a pointer that points to a deleted (or freed) memory location. Pointer can function as a dangling pointer in three ways. 1. De-allocation of memory C++ Code // Deallocating a memory pointed by ptr causes // dangling pointer #include <cstdlib> #include <iostream> int main() { int* ptr = (int *)malloc(sizeof(int)); //...
3 min read
In this article, you will learn about the adjacency list in C++ with its different methods and implimentations. Graph Representation: A graph is a collection of nodes (vertices) and edges that connect these nodes. Graphs can be categorized into various types, including directed and undirected graphs, weighted and...
22 min read
Robust C++ programs typically include exception handling. During the execution of a program, when errors or exceptional conditions happen, C++ enables users to treat them in a beautiful way by utilizing try, throw, and catch statements. One essential component of this mechanism is the what() function,...
3 min read
We all know that learning data types in C/C++ or any other programming language, for that matter, is essential. As keep using them all the time in our coding and career as software engineer journey. Every data type will be associated with a specific size and memory,...
3 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