STLdeque的emplace方法(12)


STLdeque的emplace方法(12)

原文地址:http://www.cplusplus.com/reference/deque/deque/emplace/
public member function
<deque>

std::deque::emplace

template <class... Args>
  iterator emplace (const_iterator position, Args&&... args);
Construct and insert element

The container is extended by inserting a new element at position. This new element is constructed in place using args as the arguments for its construction.

通过在指定位置插入一个新的元素以扩展容器,新的元素的值使用参数args传递给元素的构造器构造。


This effectively increases the container size by one.

这是一种高效的增加容器大小的方法。


Double-ended queues are designed to be efficient performing insertions (and removals) from either the end or the beginning of the sequence. Insertions on other positions are usually less efficient than in list or forward_list containers.
See emplace_front and emplace_back for
member functions that extend the container directly at the beginning or at the end.

双端队列设计是为了能高效地在开头以及结尾插入以及删除元素的序列,在其他位置插入元素通常表现比list以及forward_list更差。



The element is constructed in-place by calling allocator_traits::construct with args forwarded.


Parameters

position
Position in the container where the new element is inserted.

Member type const_iterator is a random access iterator type that points to a constant element.

新元素插入的位置。

args
Arguments forwarded to construct the new element.

构造新元素的参数。

Return value

An iterator that points to the newly emplaced element.

返回插入新元素的迭代器。


Member type iterator is a random access iterator type that points to an element.

迭代器属于随机访问迭代器。


The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocatorbad_alloc is
thrown if the allocation request does not succeed).

如果发生了重分配,将使用容器的分配器进行内存分配,这可能会抛出异常。(例如allocator这个默认的分配器在请求失败时会抛出bad_alloc异常)


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// deque::emplace
#include <iostream>
#include <deque>

int main ()
{
  std::deque<int> mydeque = {10,20,30};

  auto it = mydeque.emplace ( mydeque.begin()+1, 100 );
  mydeque.emplace ( it, 200 );
  mydeque.emplace ( mydeque.end(), 300 );

  std::cout << "mydeque contains:";
  for (auto& x: mydeque)
    std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}


Output:

mydeque contains: 10 200 100 20 30 300



Complexity

Depending on the particular library implemention, up to linear in the number of elements between position and one of the ends of thedeque.



Iterator validity

If the insertion happens at the beginning or the end of the sequence, all iterators related to this container are invalidated, but pointers and references remain valid, referring to the same elements they were referring to before
the call.


If the insertion happens anywhere else in the deque, all iterators, pointers and references related to this container
are invalidated.


Data races

The container is modified.
If the insertion happens at the beginning or the end of the sequence, no contained elements are accessed (although see iterator validity above).
If it happens anywhere else, it is not safe to concurrently access elements.


Exception safety

If position is begin or end,
there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
If allocator_traits::construct is not supported with the appropriate arguments, or if position is not valid, it causes undefined
behavior
.

——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

//今后的翻译将以简洁为主,翻译其主要意思,一些重复率太高的也将不再翻译,只翻译重要的部分。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-9-1

于GDUT

——————————————————————————————————————————————————————————————————


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注