Home  >  Article  >  Backend Development  >  C++ program to convert vector to list

C++ program to convert vector to list

WBOY
WBOYforward
2023-09-10 08:49:031327browse

C++ program to convert vector to list

Vectors in C are dynamic arrays and can contain any type of data, either user-defined or primitive. Dynamic means that the size of the vector can increase or decrease based on operations. Vectors support various functions and data manipulation is very easy. On the other hand, a list is the same container as a vector, but compared to the array implementation of vectors, the list implementation is based on a doubly linked list. Lists provide the same constant-time operation everywhere in them, which is the main feature of using lists. Let's look at the main methods of converting vectors to lists.

Use range constructor

To use the range constructor, you must pass the start and end pointers of the vector as parameters to the constructor when creating the list.

grammar

vector <int> ip;
list <int> op( ip.begin(), ip.end() );

algorithm

  • Store the input in a vector.
  • Pass the start and end pointers of the vector to the list's range constructor.
  • 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

Use the assign function of std::list

The usage of std::list is similar to that of the range constructor. We pass the start and end pointers of the vector in the same way as the range constructor.

grammar

vector <int> ip;
list <int> op();
op.assign(ip.begin(), ip.end());

algorithm

  • Store the input in a vector.
  • Define a new list.
  • Pass the start pointer and end 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 

Use list insertion function

We can use the insert function of the list to insert data from the vector into the list. Lists require pointers to the start of the list and pointers to the start and end of the vector.

grammar

vector <int> ip;
list <int> op();
op.insert(op.begin(), ip.begin(), ip.end());

algorithm

  • Store the input in a vector.
  • Define a new list.
  • Pass the start pointer of the list and the start and end pointers
  • A vector of list insertion functions.
  • 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 

in conclusion

In C, converting a vector to a list has the benefit of uniform operation complexity anywhere in the list. There are several ways to convert a vector into a list. However, we have only mentioned the simplest and fastest methods here.

The above is the detailed content of C++ program to convert vector to list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete