STLvector中的insert方法(28)


STLvector中的insert方法(28)

public member function
<vector>

std::vector::insert

single element (1)
iterator insert (const_iterator position, const value_type& val);
fill (2)
iterator insert (const_iterator position, size_type n, const value_type& val);
range (3)
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);

move (4)
iterator insert (const_iterator position, value_type&& val);
initializer list (5)
iterator insert (const_iterator position, initializer_list<value_type> il);
相应的例子及运行截图:
(1)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"at first vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	vi.insert(vi.begin()+1,15);
	cout<<"after insert(vi.begin()+1,15)  vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
}

运行截图:


(2)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={10,20,30};
	cout<<"at first vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	vi.insert(vi.begin()+1,3,15);
	cout<<"after insert(vi.begin()+1,15)  vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;

}


(3)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> v2={10,20,30,40,50};
	vector<int> vi={1,2,3};
	cout<<"at first vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	vi.insert(vi.end(),v2.begin()+1,v2.end()-1);
	cout<<"after insert(vi	.end(),v2.begin()+1,v2.end()-1)  vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;

}


(4)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={1,2,3};
	int &&r=10;
	cout<<"at first vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	cout<<"&&r=10,r="<<r<<endl;
	vi.insert(vi.end(),r);
	cout<<"after insert(vi.end(),r)  vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	cout<<"&&r=10,r="<<r<<endl;
}


(5)

#include <iostream>
#include <vector>
#include <algorithm>
#include <initializer_list>
using namespace std;
int main(int argc,char **argv)
{
	vector<int> vi={1,2,3};
	cout<<"at first vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
	vi.insert(vi.end(),{10,20,30,40});
	cout<<"after insert(vi.end(),{10,20,30,40})  vi:";
	for_each(vi.begin(),vi.end(),[](const int n){cout<<n<<" ";});
	cout<<endl;
}


Insert elements

The vector is extended by inserting new elements before the element at the specified position,
effectively increasing the container size by the number of elements inserted.
通过在position的前面插入元素使vector增长,这是一种高效的插入元素的方式。

This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses the current
vector capacity.
如果增长后的vector的大小超过了现在vector的capacity,那么将会自动发生重分配。

Because vectors use an array as their underlying storage, inserting elements in positions other than the vector end causes the
container to relocate all the elements that were after position to their new positions. This is generally an inefficient operation compared to the one performed for the same operation by other kinds of sequence containers (such as list or forward_list).
因为vector是使用array作为底层的存储容器,在指定位置插入元素代表着容器将要移动所有position后面的元素到一个新的位置。这通常都是很低效率的事情(相对于另一种更高效的插入序列容器,例如list或者forward_list),emplace_back则是一种直接在数组最后位置扩展的方法。

The parameters determine how many elements are inserted and to which values they are initialized:
参数决定了插入多少个元素以及他们的初始值。

Parameters

position
Position in the vector where the new elements are inserted.
iterator is a member type, defined as a random access iterator type that points to elements.
position是新元素插入的位置.
iterator是一个随机访问迭代器。
val
Value to be copied (or moved) to the inserted elements.
Member type value_type is the type of the elements in the container, defined in deque as an alias of its first template parameter
(T).
val是插入元素的值(通过拷贝或者是移动构造)
其类型和容器内元素类型一致。由vector模版参数决定。
n
Number of elements to insert. Each element is initialized to a copy of val.
Member type size_type is an unsigned integral type.
n是插入元素的数目。每一个元素的值都从val中拷贝而来。
n是以俄国unsigned integral.
first, last
Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted at position (in the same order).
指定范围内的迭代器。将从该范围([first,last))依次顺序地拷贝其元素插入到指定位置(position)的vector.

Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
需要注意的是该范围包括first到last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
The function template argument InputIterator shall be an input iterator type that points to elements of a type from
which value_type objects can be constructed.
该方法中的模版参数InputIterator应该是一个输入迭代器。
il
An initializer_list object. Copies of these elements are inserted
at position (in the same order).
一个初始化列表对象,复制其元素到插入位置。
These objects are automatically constructed from initializer list declarators.
这些对象都自动从初始化列表中构造。
Member type value_type is the type of the elements in the container, defined in vector as an alias of its first template
parameter (T).
值类型由vector的模版参数指定,和容器内元素类型一致。



Return value

An iterator that points to the first of the newly inserted elements.
返回值为插入的第一个新元素的位置。

Member type iterator is a random access iterator type that points to elements.
其类型为随机访问迭代器。

If reallocations happen, 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_allocator)

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// inserting into a vector
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (3,100);
  std::vector<int>::iterator it;

  it = myvector.begin();
  it = myvector.insert ( it , 200 );

  myvector.insert (it,2,300);

  // "it" no longer valid, get a new one:
  it = myvector.begin();

  std::vector<int> anothervector (2,400);
  myvector.insert (it+2,anothervector.begin(),anothervector.end());

  int myarray [] = { 501,502,503 };
  myvector.insert (myvector.begin(), myarray, myarray+3);

  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}


Output:

myvector contains: 501 502 503 300 300 400 400 200 100 100 100





Complexity

Linear on the number of elements inserted (copy/move construction) plus the number of elements after position (moving).与插入元素(构造或者移动构造)的数目加上插入位置元素后面的元素(移动)的数目线性相关。

Additionally, if InputIterator in the range insert (3) is not at least of a forward iterator category (i.e.,
just an input iterator) the new capacity cannot be determined beforehand and the insertion incurs in additional logarithmic
complexity in size (reallocations).此外,如果在第三中情况range insert(3)下的inputIterator不至少是一个正向迭代器(例如只是一个输入迭代器),新的容量不能被事先决定并且将额外导致对数时间的复杂度。//感觉这后面的一句翻译的不太对,希望有大牛来指导一二。

If a reallocation happens, the reallocation is itself up to linear in the entire size at the moment of the reallocation.
如果发生重分配,将额外导致与元素总数目线性相关的时间复杂度。

Iterator validity

If a reallocation happens, all iterators, pointers and references related to the container are invalidated.如果发生重分配,所有的迭代器,指针以及引用都将失效。
Otherwise, only those pointing to position and beyond are invalidated, with all iterators, pointers and references to elements beforeposition guaranteed to keep referring to the same elements they were referring to before the call.

否则,只有那些指向position后面的元素的才会失效,前面的依然有效。//(翻译不会逐字逐句翻译,只会翻译其对应的语义)


Data races

All copied elements are accessed.
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 (although see iterator validity above).
否则,position前面的元素不会被访问,同时访问以及修改他们都是安全的。



Exception safety

If the operation inserts a single element at the end, and no reallocations happen, there are no
changes in the container in case of exception (strong guarantee). 
如果在end的位置之前插入单一的元素,并且不发生重分配,那么抛出异常的规则不变。
In case of reallocations, the strong guarantee is also given in this case 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 for the element constructions,
or if an invalid position or range is specified, it causes undefined behavior.
如果内存分配器不支持元素的构造,或者插入的位置或者范围无效,将导致未知的错误。

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

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

2014-8-18

于GDUT




发表回复

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