📜  如何在 C++ STL List 中插入元素?

📅  最后修改于: 2022-05-13 01:57:14.438000             🧑  作者: Mango

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

List已经在很多文章中讨论过,但本文的唯一目的是涵盖可能在列表容器中进行的所有类型的插入,并详细介绍插入操作。
List 及其许多函数都定义在头文件“list”下。下面讨论各种列表插入功能。

使用赋值()

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

  • 在列表中一次插入多个元素。
    语法: list.assign(number of times, element)
  • 将 1 个列表的元素复制到另一个列表中。
    语法: list.assign(lis2.begin(),lis2.end())
  • 将数组元素复制到列表中。
    语法: list.assign(arr,arr+size)。
// C++ code to demonstrate the working of assign()
  
#include 
#include  // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list list1;
    list list2;
    list 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::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::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::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 
#include  // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list 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::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::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 
#include  // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list 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::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::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(pos_iter,ele_num,ele) : insert() 用于列表的任何位置插入元素

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

  • 使用 emplace(pos_iter,ele) :以与 insert() 类似的方式工作,但值是在容器的前面位置就地构造的,在 push_front 中,首先创建一个对象,然后将其复制到容器中。并且只允许在 1 次插入 1 个值。
// C++ code to demonstrate the working of 
// insert() and emplace()
  
#include 
#include  // for list operations
using namespace std;
  
int main() 
{
    // declaring list
    list list1;
      
    // using assign() to insert multiple numbers
    // creates 3 occurrences of "2"
    list1.assign(3,2);
      
    // initializing list iterator to beginning
    list::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::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::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::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