如何在C++ STL列表中插入元素?

列表 已经在许多文章中讨论过,但本文的唯一目的是涵盖列表容器中可能携带的所有类型的插入,并提供有关插入操作的详细信息。 List及其许多函数都是在头文件“List”下定义的。下面讨论各种列表插入函数。

null
使用assign()

assign()函数用于 在一次操作中在列表中插入多个元素。 “assign()”的工作方式如下:

  • 在列表中同时插入多个元素。 语法: 列表分配(次数、元素) .
  • 将一个列表中的元素复制到另一个列表中。 语法: 列表赋值(lis2.begin(),lis2。end())
  • 将数组元素复制到列表中。 语法: 列表分配(arr,arr+大小)。

// C++ code to demonstrate the working of assign()
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
// declaring list
list< int > list1;
list< int > list2;
list< int > list3;
// initializing array
int arr[10] = { 1, 2, 3, 4 };
// using assign() to insert multiple numbers
// creates 4 occurrences of "2"
list1.assign(4,2);
// Printing the assigned list
cout << "The list after inserting multiple elements is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
// using assign() to copy elements of list to other
// assigns 4 occurrences of "2"
list2.assign(list1.begin(),list1.end());
// Printing the assigned list
cout << "The list after copying list elements is : " ;
for (list< int >::iterator i=list2.begin(); i!=list2.end(); i++)
cout << *i << " " ;
cout << endl;
// using assign() to copy elements of array to list
// assigns array elements
list3.assign(arr,arr+4);
// Printing the assigned list
cout << "The list after copying array elements is : " ;
for (list< int >::iterator i=list3.begin(); i!=list3.end(); i++)
cout << *i << " " ;
cout << endl;
}


输出:

The list after inserting multiple elements is : 2 2 2 2 
The list after copying list elements is : 2 2 2 2 
The list after copying array elements is : 1 2 3 4 

开头插入

  • 使用push_front(): push_front()用于 在开头插入元素 名单的一部分。将列表大小增加1。
  • 使用emplace_front(): 与push_front的工作方式类似,但 值在前面的位置构建到位 对于容器,在push_front中,首先创建一个对象,然后将其复制到容器中。

// C++ code to demonstrate the working of
// push_front() and emplace_front()
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
// declaring list
list< int > list1;
// using assign() to insert multiple numbers
// creates 2 occurrences of "2"
list1.assign(2,2);
// using push_front to insert elements at beginning
// inserts 5 at beginning
list1.push_front(5);
// Printing the new list
cout << "The list after inserting elements using push_front is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
// using emplace_front to insert elements at beginning
// inserts 7 at beginning
list1.emplace_front(7);
// Printing the new list
cout << "The list after inserting elements using emplace_front is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
}


输出:

The list after inserting elements using push_front is : 5 2 2 
The list after inserting elements using emplace_front is : 7 5 2 2 

末端插入

  • 使用push_back(): push_back()用于 在末尾插入元素 名单的一部分。将列表大小增加1。
  • 使用emplace_back(): 其工作方式与push_back类似,但 值在后位置构建到位 对于容器,在push_back中,首先创建一个对象,然后将其复制到容器中。

// C++ code to demonstrate the working of
// push_back() and emplace_back()
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
// declaring list
list< int > list1;
// using assign() to insert multiple numbers
// creates 2 occurrences of "2"
list1.assign(2,2);
// using push_back to insert elements at the end
// inserts 5 at end
list1.push_back(5);
// Printing the new list
cout << "The list after inserting elements using push_back is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
// using emplace_back to insert elements at the end
// inserts 7 at end
list1.emplace_back(7);
// Printing the new list
cout << "The list after inserting elements using emplace_back is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
}


输出:

The list after inserting elements using push_back is : 2 2 5 
The list after inserting elements using emplace_back is : 2 2 5 7 

在任何位置插入

  • 使用插入(位置、数字、数字): insert()用于 在任何位置插入元件 名单的一部分。

    . 此函数包含3个元素:位置、要插入的元素数和要插入的值 。如果未提及,元素数默认设置为1。

  • 使用emplace(pos_iter,ele): 其工作方式与insert()类似,但 值在前面的位置构建到位 对于容器,在push_front中,首先创建一个对象,然后将其复制到容器中。一次只允许插入一个值。

// C++ code to demonstrate the working of
// insert() and emplace()
#include <iostream>
#include <list> // for list operations
using namespace std;
int main()
{
// declaring list
list< int > list1;
// using assign() to insert multiple numbers
// creates 3 occurrences of "2"
list1.assign(3,2);
// initializing list iterator to beginning
list< int >::iterator it = list1.begin();
// iterator to point to 3rd position
advance(it,2);
// using insert to insert 1 element at the 3rd position
// inserts 5 at 3rd position
list1.insert(it,5);
// Printing the new list
cout << "The list after inserting 1 element using insert() is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
// using insert to insert 2 element at the 4th position
// inserts 2 occurrences of 7 at 4th position
list1.insert(it,2,7);
// Printing the new list
cout << "The list after inserting multiple elements using insert() is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
cout << endl;
// using emplace to insert elements at the 6th position
// inserts 8 at 6th position
list1.emplace(it,8);
// Printing the new list
cout << "The list after inserting elements using emplace() is : " ;
for (list< int >::iterator i=list1.begin(); i!=list1.end(); i++)
cout << *i << " " ;
}


输出:

The list after inserting 1 element using insert() is : 2 2 5 2 
The list after inserting multiple elements using insert() is : 2 2 5 7 7 2 
The list after inserting elements using emplace() is : 2 2 5 7 7 8 2 

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享