跳到主要内容

std::deque insert() 方法

// (1) Non const version only
iterator insert( const_iterator pos, const T& value );

// (2) Non const version only
iterator insert( const_iterator pos, T&& value );

// (3) Non const version only
iterator insert( const_iterator pos, size_type count, const T& value );

// (4) Non const version only
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );

// (5) Non const version only
iterator insert( const_iterator pos, std::initializer_list<T> ilist );

将元素插入到容器中,如果容器中还没有包含具有等效键的元素。

  • (1-2)pos 之前插入 value

  • (3)pos 之前插入 count 个值的副本。

  • (4)pos 之前插入来自范围 [ first; last ) 的元素。

    此重载仅在 InputIt 符合 LegacyInputIterator 要求时参与重载决议,以避免与重载 (3) 产生歧义。

    未定义行为

    如果 firstlast 是指向 *this 的迭代器,则行为未定义。

  • (5)pos 之前插入初始化列表 ilist 中的元素。

    未定义行为

    所有迭代器,包括末尾迭代器,都将失效。引用也将失效,除非 pos == begin()pos == end(),在这种情况下它们不会失效。

参数

  • pos - 将在其之前插入内容的迭代器。pos 可以是 end() 迭代器
  • value - 要插入的元素值
  • count - 要插入的元素数量
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表

类型要求

返回值

  • (1-2) 指向插入值的迭代器。
  • (3) 指向第一个插入元素的迭代器,如果 count == 0 则为 pos
  • (4) 指向第一个插入元素的迭代器,如果 first == last 则为 pos
  • (5) 指向第一个插入元素的迭代器,如果 ilist 为空则为 pos

复杂度

对于以下所有情况,Nstd::min(std::distance(pos, begin()), std::distance(pos, end()))

  • (1-2)pos 与容器任一端点之间较短距离的线性时间复杂度 - O(N)
  • (3) count 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(count + N)
  • (4) std::distance(first, last) 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(std::distance(first, last) + N)
  • (5) ilist.size() 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(ilist.size() + N)

异常

如果在任一端插入单个元素时抛出异常,此函数无副作用(强异常保证)。

示例

Main.cpp
#include <iostream>
#include <iterator>
#include <deque>

void print(int id, const std::deque<int>& container)
{
std::cout << id << ". ";
for (const int x: container)
std::cout << x << ' ';
std::cout << '\n';
}

int main ()
{
std::deque<int> c1(3, 100);
print(1, c1);

auto it = c1.begin();
it = c1.insert(it, 200);
print(2, c1);

c1.insert(it, 2, 300);
print(3, c1);

// reset `it` to the begin:
it = c1.begin();

std::deque<int> c2(2, 400);
c1.insert(std::next(it, 2), c2.begin(), c2.end());
print(4, c1);

int arr[] = {501, 502, 503};
c1.insert(c1.begin(), arr, arr + std::size(arr));
print(5, c1);

c1.insert(c1.end(), {601, 602, 603});
print(6, c1);
}
可能输出
1. 100 100 100
2. 200 100 100 100
3. 300 300 200 100 100 100
4. 300 300 400 400 200 100 100 100
5. 501 502 503 300 300 400 400 200 100 100 100
6. 501 502 503 300 300 400 400 200 100 100 100 601 602 603
本文源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。

std::deque insert() 方法

// (1) Non const version only
iterator insert( const_iterator pos, const T& value );

// (2) Non const version only
iterator insert( const_iterator pos, T&& value );

// (3) Non const version only
iterator insert( const_iterator pos, size_type count, const T& value );

// (4) Non const version only
template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last );

// (5) Non const version only
iterator insert( const_iterator pos, std::initializer_list<T> ilist );

将元素插入到容器中,如果容器中还没有包含具有等效键的元素。

  • (1-2)pos 之前插入 value

  • (3)pos 之前插入 count 个值的副本。

  • (4)pos 之前插入来自范围 [ first; last ) 的元素。

    此重载仅在 InputIt 符合 LegacyInputIterator 要求时参与重载决议,以避免与重载 (3) 产生歧义。

    未定义行为

    如果 firstlast 是指向 *this 的迭代器,则行为未定义。

  • (5)pos 之前插入初始化列表 ilist 中的元素。

    未定义行为

    所有迭代器,包括末尾迭代器,都将失效。引用也将失效,除非 pos == begin()pos == end(),在这种情况下它们不会失效。

参数

  • pos - 将在其之前插入内容的迭代器。pos 可以是 end() 迭代器
  • value - 要插入的元素值
  • count - 要插入的元素数量
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表

类型要求

返回值

  • (1-2) 指向插入值的迭代器。
  • (3) 指向第一个插入元素的迭代器,如果 count == 0 则为 pos
  • (4) 指向第一个插入元素的迭代器,如果 first == last 则为 pos
  • (5) 指向第一个插入元素的迭代器,如果 ilist 为空则为 pos

复杂度

对于以下所有情况,Nstd::min(std::distance(pos, begin()), std::distance(pos, end()))

  • (1-2)pos 与容器任一端点之间较短距离的线性时间复杂度 - O(N)
  • (3) count 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(count + N)
  • (4) std::distance(first, last) 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(std::distance(first, last) + N)
  • (5) ilist.size() 的线性时间复杂度 + 在 pos 与容器任一端点之间较短距离的线性时间复杂度 - O(ilist.size() + N)

异常

如果在任一端插入单个元素时抛出异常,此函数无副作用(强异常保证)。

示例

Main.cpp
#include <iostream>
#include <iterator>
#include <deque>

void print(int id, const std::deque<int>& container)
{
std::cout << id << ". ";
for (const int x: container)
std::cout << x << ' ';
std::cout << '\n';
}

int main ()
{
std::deque<int> c1(3, 100);
print(1, c1);

auto it = c1.begin();
it = c1.insert(it, 200);
print(2, c1);

c1.insert(it, 2, 300);
print(3, c1);

// reset `it` to the begin:
it = c1.begin();

std::deque<int> c2(2, 400);
c1.insert(std::next(it, 2), c2.begin(), c2.end());
print(4, c1);

int arr[] = {501, 502, 503};
c1.insert(c1.begin(), arr, arr + std::size(arr));
print(5, c1);

c1.insert(c1.end(), {601, 602, 603});
print(6, c1);
}
可能输出
1. 100 100 100
2. 200 100 100 100
3. 300 300 200 100 100 100
4. 300 300 400 400 200 100 100 100
5. 501 502 503 300 300 400 400 200 100 100 100
6. 501 502 503 300 300 400 400 200 100 100 100 601 602 603
本文源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”查看本文档的所有更改。
悬停查看原始许可证。