Table of Contents
Use range constructor
grammar
algorithm
Example
Output
Use the assign function of std::list
Use list insertion function
in conclusion
Home Backend Development C++ C++ program to convert vector to list

C++ program to convert vector to list

Sep 10, 2023 am 08:49 AM
list Convert vector

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Practical tips for converting full-width English letters into half-width form Practical tips for converting full-width English letters into half-width form Mar 26, 2024 am 09:54 AM

Practical tips for converting full-width English letters into half-width forms. In modern life, we often come into contact with English letters, and we often need to input English letters when using computers, mobile phones and other devices. However, sometimes we encounter full-width English letters, and we need to use the half-width form. So, how to convert full-width English letters to half-width form? Here are some practical tips for you. First of all, full-width English letters and numbers refer to characters that occupy a full-width position in the input method, while half-width English letters and numbers occupy a full-width position.

Golang time processing: How to convert timestamp to string in Golang Golang time processing: How to convert timestamp to string in Golang Feb 24, 2024 pm 10:42 PM

Golang time conversion: How to convert timestamp to string In Golang, time operation is one of the very common operations. Sometimes we need to convert the timestamp into a string for easy display or storage. This article will introduce how to use Golang to convert timestamps to strings and provide specific code examples. 1. Conversion of timestamps and strings In Golang, timestamps are usually expressed in the form of integer numbers, which represent the number of seconds from January 1, 1970 to the current time. The string is

How to convert AI files to CDR format How to convert AI files to CDR format Feb 19, 2024 pm 04:09 PM

AI files refer to vector graphics files created by Adobe Illustrator (AI for short) software, while CDR files refer to vector graphics files created by CorelDRAW software. Since these two softwares are developed by different manufacturers, their file formats are different and cannot be directly converted to each other. However, we can convert AI files to CDR files through some methods. A commonly used conversion method will be introduced below. Step 1: Export AI files to EPS format AdobeIllust

How to convert a virtual machine to a physical machine? How to convert a virtual machine to a physical machine? Feb 19, 2024 am 11:40 AM

Converting a virtual machine (VM) to a physical machine is the process of migrating a virtual instance and associated application software to a physical hardware platform. This conversion helps optimize operating system performance and hardware resource utilization. This article aims to provide an in-depth look at how to make this conversion. How to implement migration from virtual machine to physical machine? Typically, the conversion process between a virtual machine and a physical machine is performed outside the virtual machine by third-party software. This process consists of multiple stages involving the configuration of virtual machines and the transfer of resources. Prepare the physical machine: The first step is to ensure that the physical machine meets the hardware requirements for Windows. We need to back up the data on a physical machine as the conversion process will overwrite the existing data. *Username and password for an administrator account with administrator rights to create system images. will be virtual

Detailed explanation of the implementation method of converting PHP months to English months Detailed explanation of the implementation method of converting PHP months to English months Mar 21, 2024 pm 06:45 PM

This article will introduce in detail how to convert months in PHP to English months, and give specific code examples. In PHP development, sometimes we need to convert digital months to English months, which is very practical in some date processing or data display scenarios. The implementation principles, specific code examples and precautions will be explained in detail below. 1. Implementation principle In PHP, you can convert digital months into English months by using the DateTime class and format method. Date

How to convert ODT to Word in Windows 11/10? How to convert ODT to Word in Windows 11/10? Feb 20, 2024 pm 12:21 PM

In this article, we will show you how to convert OpenDocumentTextDocument (ODT) files to Microsoft Word (Docx, DOC, etc.). Format. How to Convert ODT to Word in Windows 11/10 Here is how you can convert ODT documents to DOC or DOCX format on Windows PC: Convert ODT to Word using WordPad or Word The first method we are going to show you Is to use WordPad or MicrosoftWord to convert ODT to Word. Here are the steps to achieve this: First, open the WordPad app using the Start menu. Now, go to

How to convert full-width English letters into half-width letters How to convert full-width English letters into half-width letters Mar 25, 2024 pm 02:45 PM

How to convert full-width English letters into half-width letters In daily life and work, sometimes we encounter situations where we need to convert full-width English letters into half-width letters, such as when entering computer passwords, editing documents, or designing layouts. Full-width English letters and numbers refer to characters with the same width as Chinese characters, while half-width English letters refer to characters with a narrower width. In actual operation, we need to master some simple methods to convert full-width English letters into half-width letters so that we can process text and numbers more conveniently. 1. Full-width English letters and half-width English letters

Hand-tearing Llama3 layer 1: Implementing llama3 from scratch Hand-tearing Llama3 layer 1: Implementing llama3 from scratch Jun 01, 2024 pm 05:45 PM

1. Architecture of Llama3 In this series of articles, we implement llama3 from scratch. The overall architecture of Llama3: Picture the model parameters of Llama3: Let's take a look at the actual values ​​of these parameters in the Llama3 model. Picture [1] Context window (context-window) When instantiating the LlaMa class, the variable max_seq_len defines context-window. There are other parameters in the class, but this parameter is most directly related to the transformer model. The max_seq_len here is 8K. Picture [2] Vocabulary-size and AttentionL

See all articles