首頁 > 後端開發 > C++ > 主體

C++程式將向量轉換為列表

WBOY
發布: 2023-09-10 08:49:03
轉載
1300 人瀏覽過

C++程式將向量轉換為列表

C 中的向量是動態數組,可以包含任何類型的數據,可以是使用者定義的或原始的。動態是指向量的大小可以根據操作增加或減少。向量支援各種函數,資料操作非常容易。另一方面,列表是與向量相同的容器,但與向量的陣列實作相比,列表實作是基於雙向鍊錶的。清單在其中的任何位置都提供相同的恆定時間操作,這是使用清單的主要功能。讓我們來看看將向量轉換為列表的主要方法。

使用範圍建構函數

要使用範圍建構函數,在建立列表時必須將向量的起始指標和結束指標作為參數傳遞給建構函數。

文法

vector <int> ip;
list <int> op( ip.begin(), ip.end() );
登入後複製

演算法

  • 將輸入儲存在向量中。
  • 將向量的起始指標和結束指標傳遞給列表的範圍建構子。
  • 顯示清單的內容。

範例

#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;
}
登入後複製

輸出

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
登入後複製

使用std::list的assign函數

std::list的用法與範圍建構子的用法類似。我們以與範圍建構函數相同的方式傳遞向量的起始和結束指標。

文法

vector <int> ip;
list <int> op();
op.assign(ip.begin(), ip.end());
登入後複製

演算法

  • 將輸入儲存在向量中。
  • 定義一個新的清單。
  • 將向量的起始指標和結束指標傳遞給列表的assign函數
  • 顯示清單的內容。

範例

#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;
}
登入後複製

輸出

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 
登入後複製

使用列表插入函數

我們可以使用列表的插入函數將資料從向量插入到列表中。列表需要列表開頭的指標以及向量開頭和結尾的指標。

文法

vector <int> ip;
list <int> op();
op.insert(op.begin(), ip.begin(), ip.end());
登入後複製

演算法

  • 將輸入儲存在向量中。
  • 定義一個新的清單。
  • 傳遞清單的起始指標以及起始和結束指標
  • 列表插入函數的向量。
  • 顯示清單的內容。

範例

#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;
}
登入後複製

輸出

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 
登入後複製

結論

在C 中,將向量轉換為列表具有在列表的任何位置上統一的操作複雜度的好處。有幾種方法可以將向量轉換為列表。然而,我們只在這裡提到了最簡單和最快的方法。

以上是C++程式將向量轉換為列表的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!