日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c ++ stl_通过分配另一个列表的所有元素来创建列表| C ++ STL

發(fā)布時(shí)間:2023/12/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c ++ stl_通过分配另一个列表的所有元素来创建列表| C ++ STL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c ++ stl

There are two methods to implement it...

有兩種方法可以實(shí)現(xiàn)它...

1 ) by assigning the existing list direct to the new list

1)通過直接將現(xiàn)有列表分配給新列表

list<int> list2 = list1;

2 ) by using list::assign() function

2)通過使用list :: assign()函數(shù)

list<int> list2;list2.assign(list1.begin(), list1.end());

Example:

例:

In this example, we have a list list1 with 10 elements and we are creating another list list2, the elements of list2 will be assigning through the list1 by using list:assign() function, which is a library function of "list header". And, we are also creating a list list3 that is going to be assigned directly through the list1.

在這個(gè)例子中,我們有具有10個(gè)元素的列表list1的和我們正在創(chuàng)建另一個(gè)列表list2中 ,list2中的元素將通過使用列表中的列表1被分配:分配()函數(shù),該函數(shù)是“列表頭”的庫函數(shù)。 并且,我們還創(chuàng)建了一個(gè)列表list3 ,它將直接通過list1進(jìn)行分配。

Program:

程序:

#include <iostream> #include <list> using namespace std;//function to display the list void dispList(list<int> L) {//declaring iterator to the listlist<int>::iterator l_iter;for (l_iter = L.begin(); l_iter != L.end(); l_iter++)cout<< *l_iter<< " ";cout<<endl; }int main() {//list1 declarationlist<int> list1 = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};//displaying list1cout<<"Size of list1: "<<list1.size()<<endl;cout<<"Elements of list1: ";dispList(list1);//declaring list2 and assigning the Elements//of list1list<int> list2;list2.assign(list1.begin(), list1.end());//displaying list2cout<<"Size of list2: "<<list2.size()<<endl;cout<<"Elements of list2: ";dispList(list2);//declaring list3 by assigning with list1list<int> list3 = list1;//displaying list3cout<<"Size of list3: "<<list3.size()<<endl;cout<<"Elements of list3: ";dispList(list3);return 0; }

Output

輸出量

Size of list1: 10 Elements of list1: 10 20 30 40 50 60 70 80 90 100 Size of list2: 10 Elements of list2: 10 20 30 40 50 60 70 80 90 100 Size of list3: 10 Elements of list3: 10 20 30 40 50 60 70 80 90 100

翻譯自: https://www.includehelp.com/stl/creating-a-list-by-assigning-the-all-elements-of-another-list.aspx

c ++ stl

總結(jié)

以上是生活随笔為你收集整理的c ++ stl_通过分配另一个列表的所有元素来创建列表| C ++ STL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。