C++ STL 中的 list assign() 函式


本任務旨在展示 C++ 中 assign() 函式的工作原理。

list::assign() 函式是 C++ 標準模板庫的一部分。它用於為列表分配值,以及將值從一個列表複製到另一個列表。

需要包含 <list> 標頭檔案才能呼叫此函式。

語法

分配新值的語法如下:

List_Name.assign(size,value)

語法

將值從一個列表複製到另一個列表的語法如下:

First_List.assign(Second_List.begin(),Second_list.end())

引數

該函式接受兩個引數:

第一個是 size,表示列表的大小;第二個是 value,表示要儲存在列表中的資料值。

返回值

該函式沒有返回值。

示例

Input: Lt.assign(3,10)
Output: The size of list Lt is 3.
The elements of the list Lt are 10 10 10.

**說明**:

以下示例演示瞭如何使用 assign() 函式為列表分配其大小和值。傳遞給 list 函式的第一個值將成為列表的大小,在本例中為 3;第二個元素是分配給列表每個位置的值,這裡為 10。

示例

Input: int array[5] = { 1, 2, 3, 4 }
Lt.assign(array,array+3)
Output: The size of list Lt is 3.
The elements of the list Lt are 1 2 3.

**說明**:

以下示例演示瞭如何使用陣列為列表分配值。將分配給列表的元素總數將成為列表的大小。

使用者只需將陣列的名稱作為 assign() 函式的第一個引數傳遞,第二個引數應為陣列的名稱,後跟“+”號以及使用者想要分配給列表的元素數量。

在上述情況下,我們寫了 3,因此陣列的前三個元素將被分配給列表。

如果我們寫入的數字大於陣列中存在的元素數量(例如 6),則程式不會顯示任何錯誤,而是列表的大小將變為 6,列表中的額外位置將分配值為零。

**以下程式中使用的演算法如下**:

  • 首先建立一個函式 ShowList(list<int> L),用於顯示列表的元素。
  • 建立一個迭代器,例如 itr,它將包含要顯示的列表的初始元素。
  • 使迴圈執行,直到 itr 達到列表的最後一個元素。
  • 然後在 main() 函式中使用 list<int> 建立三個列表,例如 L1、L2 和 L3,以便它們接受 int 型別的值,然後建立一個 int 型別的陣列,例如 arr[],併為其分配一些值。
  • 然後使用 assign() 函式為列表 L1 分配大小和一些值,然後將列表 L1 傳遞到 ShowDisplay() 函式中。
  • 然後使用 assign() 函式將列表 L1 的元素複製到 L2 中,並將列表 L2 傳遞到 ShowList() 函式中。
  • 然後使用 assign() 函式將陣列 arr[] 的元素複製到列表 L3 中,並將列表 L3 傳遞到 DisplayList() 函式中。

演算法

Start
Step 1-> Declare function DisplayList(list<int> L) for showing list elements
   Declare iterator itr
   Loop For itr=L.begin() and itr!=L.end() and itr++
   Print *itr
   End
Step 2-> In function main()
   Declare lists L1,L2,L3
   Initialize array arr[]
   Call L1.assign(size,value)
   Print L1.size();
   Call function DisplayList(L1) to display L1
   Call L2.assign(L1.begin(),L1.end())
   Print L2.size();
   Call function DisplayList(L2) to display L2
   Call L3.assign(arr,arr+4)
   Print L3.size();
   Call function DisplayList(L3) to display L3
Stop

示例

 線上演示

#include<iostream>
#include<list>
using namespace std;
int ShowList(list<int> L) {
   cout<<"The elements of the list are ";
   list<int>::iterator itr;
   for(itr=L.begin(); itr!=L.end(); itr++) {
      cout<<*itr<<" ";
   }
   cout<<"\n";
}
int main() {
   list<int> L1;
   list<int> L2;
   list<int> L3;
   int arr[10] = { 6, 7, 2, 4 };
   //assigning size and values to list L1
   L1.assign(3,20);
   cout<<"The size of list L1 is "<<L1.size()<<"\n";
   ShowList(L1);
   //copying the elements of L1 into L3
   L2.assign(L1.begin(),L1.end());
   cout<<"The size of list is L2 "<<L2.size()<<"\n";
   ShowList(L2);
   //copying the elements of arr[] into list L3
   L3.assign(arr,arr+4);
   cout<<"The size of list is L3 "<<L3.size()<<"\n";
   ShowList(L3);
   return 0;
}

輸出

如果我們執行以上程式碼,它將生成以下輸出:

The size of list L1 is 3
The elements of the list are 20 20 20
The size of list L2 is 3
The elements of the list are 20 20 20
The size of list L3 is 4
The elements of the list are 6 7 2 4

解釋

對於列表 L1,我們在 assign() 函式中將大小設定為 3,值設定為 20,從而生成了以上輸出。

然後我們將列表 L1 的元素複製到 L2 中,這使得 L2 的大小和值與 L1 相同,如輸出所示。

然後我們使用 assign 函式複製了陣列 arr[] 的所有元素,這使得 L3 的大小等於 4,並且其元素與陣列中的元素相同,如輸出所示。

更新於:2020 年 1 月 20 日

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.