
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Convert Vector to a List
Vectors in C++ are dynamic arrays that can contain any type of data, it can be user-defined or primitive. Dynamic is in the sense that the size of a vector can increase or decrease according to the operations. Vectors have support for various functions, for which data manipulation is very easy. Lists on the other hand are containers same as vectors, but list implementation is based on doubly linked lists compared to the array implementation of vectors. Lists offer the same constant time operations anywhere in it, that is the main feature of using lists. We take a look at the major ways to convert a vector into a list.
Using Range Constructor
To use the range constructor, the beginning and the ending pointer of the vector has to be passed as an argument to the constructor while creating the list.
Syntax
vector <int> ip; list <int> op( ip.begin(), ip.end() );
Algorithm
- Take input in a vector.
- Pass the beginning and the ending pointer of the vector to the range constructor of the list.
- Display the contents of the list.
Example
#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op( ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 15, 20, 65, 30, 24, 33, 12, 29, 36, 58, 96, 88, 30, 71 } ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }
Output
The input vector is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71 The output list is: 15 20 65 30 24 33 12 29 36 58 96 88 30 71
Using the assign function of std::list
The usage of std::list is similar to the usage of the range constructor. We pass the beginning and end pointer of the vector the same way as we passed in the range constructor.
Syntax
vector <int> ip; list <int> op(); op.assign(ip.begin(), ip.end());
Algorithm
- Take input in a vector.
- Define a new list.
- Pass the beginning and the ending pointer of the vector to the assign function of the list
- Display the contents of the list.
Example
#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op; op.assign( ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 40, 77, 8, 65, 92 ,13, 72, 30, 67, 12, 88, 37, 18, 23, 41} ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }
Output
The input vector is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41 The output list is: 40 77 8 65 92 13 72 30 67 12 88 37 18 23 41
Using the list insert function
We can insert data into a list from a vector using the insert function of the list. The list takes the beginning of the list pointer and the pointers of the beginning and the ending of the vector.
Syntax
vector <int> ip; list <int> op(); op.insert(op.begin(), ip.begin(), ip.end());
Algorithm
- Take input in a vector.
- Define a new list.
- Pass the beginning pointer of the list and the beginning and the ending pointer of the vector to the insert function of the list.
- Display the contents of the list.
Example
#include <iostream> #include <vector> #include <list> using namespace std; list <int> solve( vector <int> ip) { //initialise the list list <int> op; op.insert( op.begin(), ip.begin(), ip.end() ); return op; } int main() { vector <int> ip( { 30, 82, 7, 13, 69, 53, 70, 19, 73, 46, 26, 11, 37, 83} ); list <int> op = solve( ip ); //display the input cout<< "The input vector is: "; for( int i : ip ) { cout<< i << " "; } //display the output cout << "\nThe output list is: "; for( int j : op ) { cout << j << " "; } return 0; }
Output
The input vector is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83 The output list is: 30 82 7 13 69 53 70 19 73 46 26 11 37 83
Conclusion
Conversion from a vector to a list in C++ has the benefits of uniform operation complexities at any position in the list. There are a few more methods using which we can convert a vector to a list. However, we have only mentioned the simplest and fastest methods here.
- Related Articles
- Golang program to convert vector to a list
- Program to convert a Vector to List in Java
- How to convert a named vector to a list in R?
- C++ Program to Convert List to Set
- Java Program to convert a list to a read-only list
- C# program to convert a list of characters into a string
- Python program to convert a list to string
- Haskell Program to Convert List to a Map
- Java Program to convert a List to a Set
- C++ Program to Implement Vector
- Program to convert binary search tree to a singly linked list in C++?
- Java program to convert a list to an array
- Java program to convert an array to a list
- Python Program to Convert a given Singly Linked List to a Circular List
- Convert a list to string in Python program
