`
阿尔萨斯
  • 浏览: 4169673 次
社区版块
存档分类
最新评论

STL deque的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 atposition. This new element is constructed in place usingargsas the arguments for its construction.

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


This effectively increases the containersizeby 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 inlistorforward_listcontainers. Seeemplace_frontandemplace_backfor member functions that extend the container directly at the beginning or at the end.

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



The element is constructed in-place by callingallocator_traits::constructwithargsforwarded.


Parameters

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

Member typeconst_iteratoris arandom access iteratortype 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 typeiteratoris arandom access iteratortype that points to an element.

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


The storage for the new element is allocated usingallocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the defaultallocator,bad_allocis 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 betweenpositionand 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 thedeque, 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 seeiterator validityabove).
If it happens anywhere else, it is not safe to concurrently access elements.

Exception safety

Ifpositionisbeginorend, 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).
Ifallocator_traits::constructis not supported with the appropriate arguments, or ifpositionis not valid, it causesundefined behavior.

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

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

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

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

Email:coderguang@gmail.com

2014-9-1

于GDUT

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









分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics