STLvector中的emplace_back方法(24)


STLvector中的emplace_back方法(24)

public member function
<vector>

std::vector::emplace_back

template <class... Args>
  void emplace_back (Args&&... args);
Construct and insert element at the end

Inserts a new element at the end of the vector, right after its current last element. This new element is constructed
in place using args as the arguments for its constructor.

在vector的结尾插入一个新的元素,位置为当前最后一个元素的右边,元素的值使用args作为参数传递给其构造函数构造。


This effectively increases the container size by one, which causes an automatic reallocation of the allocated
storage space if -and only if- the new vector size surpasses the current vector capacity.

该方法可以快速有效率地在数组size范围内增长元素,除非当增长的元素个数大小超出了vector的ccapacity的时候才会发生重分配。


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

元素由调用allocator_traits::construct 以及参数args构造。


A similar member function exists, push_back, which either copies or moves an existing object into the container.

一个相似的方法是push_back,一样是通过拷贝或者移动一个已经存在(这也是两者最大的区别,push_back的参数对象必须是已经存在的对象)的对象到容器.

例子:

#include <iostream>
#include <vector>
using namespace std;
class text{
private:
	string str;
public:
	text(string s):str(s){
	}
	void show()const{
		cout<<str<<endl;
	}

};
int main()
{

	vector<text> vi;
	vi.emplace_back("haha");
	vi.front().show();	
	
	//vi.push_back("hehe");
	//vi.back().show();




}

结果:


尝试用push_back("hehe");

编译不通过:



Parameters

args
Arguments forwarded to construct the new element.

用于构造新元素的参数。

Return value

none.

If a reallocation happens, the storage is allocated using the container’s allocator, 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
// vector::emplace_back
#include <iostream>
#include <vector>

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

  myvector.emplace_back (100);
  myvector.emplace_back (200);

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

  return 0;
}


Output:

myvector contains: 10 20 30 100 200



Complexity

Constant (amortized time, reallocation may happen).

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 the end iterator is invalidated, and all other iterators, pointers and references to elements
are guaranteed to keep referring to the same elements they were referring to before the call.

否则,只有end得到的迭代器将失效,其他的迭代器,指针以及引用依然有效。



Data races

The container is modified.

容器将被访问。

If a reallocation happens, all contained elements are modified.

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


Otherwise, no existing element is accessed, and concurrently accessing or modifying them is safe (although see iterator validity above).

否则,元素不会被访问,同时修改和访问他们都是安全的。




Exception safety

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

如果不发生重分配,容器抛出异常的规则不变。


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, it causes undefined
behavior
.

如果allocator_traits::construct不支持该元素,将导致未知的行为。


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

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

2014-8-17

于GDUT






发表回复

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