STL vectorにおけるemplace方法(23)


原文の住所:http://www.cplusplus.com/reference/vector/vector/emplace/
public member function
std::vector:explace
template 
iterator emplace (const_iterator position, Args&&... args);
Costruct and insert element
The container is exteded by inserting a new element at position.This new element is constructed in place using アークス as the argments for its construction.
指定された位置に新しい要素を挿入して容器を拡張します.新しい要素の値はargsまたは元素はargsの値に基づいて構成されます.
This effectively increas the container size by one.
これは効率的に容器サイズを増加させる方法である.
An atomatic reallocation of the allocated storge space happens if-and only if-the new vector size surpasses the current vector capacity.
新しい配列のサイズが現在の配列のcapacityを超えたら、自動的に再割り当てが発生します.
Because vectors use an array as their undering storge,inserting elemens in positions other than the vector end causes the container to shift all the elements that were after position by one to their new positions.This is generation an inefficient operation compred to the one performed by other kids of sequence containers(such as) リスト or forward list.See emaplaceback for a member function that extens the container directly at the end.
vectorは、arrayをベースの格納容器として使用しているため、指定された位置に要素を挿入することは、コンテナがすべてのpositionの後ろの要素を移動するということを表しています.これは通常、非常に効率的なことである(他のものよりも効率的にシーケンスコンテナを挿入する、例えばlistまたはforwardulist)、empplace_backは配列の最後の位置に直接拡張する方法です.
The element is constructed in-place by carlling allocatortrits:construct with アークス forwarded.
元素はallocatorを使っています.trits:constructとargsで構成されています.
A simiar member function exists、 insert,which eigther copies or moves existing object s into the container.
同じような方法はinsertで、これは存在するオブジェクトをコピーまたは移動してコンテナに入れることです.
パラメータ
position
Position in the container where the new element is inserted.
メンバータイプ constiterator is a ラドm access iterator type that points to a const element.
新しい要素をコンテナに挿入する位置.
タイプはランダムアクセスです.気をつけますiterator.
アークス
Agments forwarded to construct the new element.
新しい要素を作成するためのパラメータです.
例:
#include 
#include 
#include 
using namespace std;
int main(int argc,char **argv)
{
	vector vi={10,20,30};
	auto it=vi.begin();
	it++;
	cout<
はスクリーンショットを実行します.
STL vector中的emplace方法(23)_第1张图片
Return value
An iterator that points to the newly emplaced element.
戻り値は、新しく挿入された要素を指すiteratorです.
メンバータイプ iterator is a ラドm access iterator type that points to an element.
タイプはランダムアクセスです.
If a reallocation happens,the storge is allocated using the container's allocator,which may throw exceptions on failure(for the default) allocator、 badcallo is thrown if the allocation request does not succereed)
再割り当てが発生した場合、容器の分配器を使ってメモリ割り当てを行うと、異常が発生する可能性があります.(例えば、allocatorというデフォルトの分配器は要求に失敗した時にbaduallocatorが投げられます.)
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// vector::emplace
#include 
#include 

int main ()
{
  std::vector myvector = {10,20,30};

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

  std::cout << "myvector contains:";
  for (auto& x: myvector)
    std::cout << ' ' << x;
  std::cout << '
'; return 0; }
Edit&Run
Output:
myvector contains: 10 200 100 20 30 300
Coplexity
Linear on the number of elements after position (moving).挿入位置の後ろの要素の数と線形に相関します.(後ろの要素を移動する必要があるので)If a reallocation happens、the reallocation is itself up to linea in the entire size.
再割当が発生すると,再割当プロセスは配列サイズ全体との線形時間の複雑さをもたらす.(コピー)
Iterator validity
If a reallocation happens、all iterators、pointers and references related to this container are invalidated.
再割り当てが発生した場合は、重ね合わせ器、ポインタ、参照は無効になります.
Otherwise、only those pointing to position and beyond are invalidated、with all iterators、pointers and references to elements before position Garanteed to keep referring to the same elemens they were referring to before the call.
そうでないと、positionを指しているものだけが無効になります.すべてのpositionの前にあるディズマリー、ポインタ、参照は依然として有効です.
Data races
The container is modified.
容器は修正されます.
If a reallocation happens、all contained elemens are modified.
再割り当てが発生すると、容器内のすべての要素が修正されます.
Otherwise、none of the elemens before position is accessed、and concurrently accessing or modifying them is safe.
そうでなければ、positionの前の要素は訪問されません.同時に修正して訪問するのは安全です.
Exception safety
If. position is end、andのreallocations happen、there areのchanges in the container in case of exception(strong gggaantee)
位置がendで、再割り当てが発生しない場合は、異常なルールは変わりません.
If a reallocation happens,the strong ggaarantee is also given if the type of the elemens is ether copyable or no-throw moveable.
再分配が発生し、元素のコピーや移動構造に異常がなく、異常が発生した場合も同様です.
Otherwise,the container is garanted to end in a valid state.
容器は最後まで有効です.
If. allocatortrits:construct is not supported with the apprate argments,or if position is not valid,it causes undefined behavior.
もしallocatorがtrits:constructはこの要素をサポートしていないか、またはpositionは有効ではないので、未知の行為を引き起こすことになります.
//翻訳のよくないところをご指導ください.下のメッセージを残したり、左上のメールアドレスをクリックしてメールしてください.私の間違いと不足を指摘してください.修正して、もっと良いのを皆さんに共有できます.ありがとうございます.
転載は出典を明記してください.http://blog.csdn.net/qq844352155
2014-8-17
GDUTで