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

C++ program to convert vector to list

WBOY
Release: 2023-09-10 08:49:03
forward
1353 people have browsed it

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() );
Copy after login

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;
}
Copy after login

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
Copy after login

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());
Copy after login

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;
}
Copy after login

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 
Copy after login

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());
Copy after login

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;
}
Copy after login

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 
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template