您的位置:首页 > 其它

STL vector中的emplace方法(23)

2014-08-17 09:22 393 查看
原文地址:http://www.cplusplus.com/reference/vector/vector/emplace/

public member function

<vector>


std::vector::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或者是元素根据args的值构造。

This effectively increases the container size by one.

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

An automatic reallocation of the allocated storage space happens if -and only if- the new vector size surpasses
the current vector capacity.

如果新的数组的大小超过了现在数组的capacity,那么将会自动发生重分配。

Because vectors use an array as their underlying storage, inserting elements 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 generally an inefficient operation compared to the one performed by other kinds of sequence containers (such as list or forward_list).
See emplace_back for a member function that extends the container directly at the end.

因为vector是使用array作为底层的存储容器,在指定位置插入元素代表着容器将要移动所有position后面的元素到一个新的位置。这通常都是很低效率的事情(相对于另一种更高效的插入序列容器,例如list或者forward_list),emplace_back则是一种直接在数组最后位置扩展的方法。

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

元素是使用allocator_traits::construct以及args来构造。

A similar member function exists, insert, which either copies or moves existing objects into
the container.

一个类似的方法是insert,这个则是通过拷贝或者移动存在的对象到容器内。


Parameters

positionPosition in the container where the new element is inserted.

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

新元素插入容器的位置。

类型为随机访问迭代器。注意是const_iterator.

argsArguments forwarded to construct the new element.用于构造新元素的参数。

例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
vector<int> vi={10,20,30};
auto it=vi.begin();
it++;
cout<<"now vi is :";
for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
vi.emplace(it,999);
cout<<endl<<"after emplace(it,999)"<<endl;
cout<<"now vi is :";
for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
vi.emplace(vi.begin(),111);
cout<<endl<<"after emplace(vi.begin(),111)"<<endl;
cout<<"now vi is :";
for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});

auto it2=vi.emplace(vi.end(),222);
cout<<endl<<"auto it2=vi.emplace(vi.end(),222)  ,it2="<<*it2<<endl;
cout<<endl<<"after emplace(vi.end(),222)"<<endl;
cout<<"now vi is :";
for_each(vi.begin(),vi.end(),[](int m){cout<<m<<" ";});
cout<<endl;

}
运行截图:




Return value

An iterator that points to the newly emplaced element.

返回值是一个指向新插入的元素的iterator。

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

类型为随机访问迭代器。

If a reallocation happens, the storage is allocated using the container's allocator,
which may throw exceptions on failure (for the default allocator, bad_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

// vector::emplace
#include <iostream>
#include <vector>

int main ()
{
std::vector<int> 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 << '\n';

return 0;
}

Edit
& Run

Output:

myvector contains: 10 200 100 20 30 300


Complexity

Linear on the number of elements after position (moving).
与插入位置后面元素的数目线性相关。(因为需要移动后面的元素)
If a reallocation happens, the reallocation is itself up to linear 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 guaranteed to keep referring to the same elements they were referring
to before the call.

否则,只有那些指向position以及更后面的将会失效,所有position之前的迭代器,指针,引用依旧有效。


Data races

The container is modified.

容器将被修改。

If a reallocation happens, all contained elements are modified.

如果发生重分配,容器内所有元素都将被修改。

Otherwise, none of the elements before position is accessed, and concurrently accessing or modifying them is safe.

否则,position之前的元素不会被访问,同时修改以及访问他们都是安全的。


Exception safety

If position is end, and no reallocations happen, there are no changes in the container
in case of exception (strong guarantee).

如果位置为end,并且不发生重分配,发生异常的规则不变。

If a reallocation happens, the strong guarantee is also given if the type of the elements is either copyable or no-throw moveable.

如果发生重分配,并且元素的拷贝或者移动构造不会抛出异常,发生异常的规则也是一样的。

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.

如果allocator_traits::construct不支持该元素,或者position不是有效的,将导致未知的行为。

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

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-17

于GDUT
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  STL STL vector