STL中的assign方法(19)


STL中的assign方法(19)

public member function
<vector>

std::vector::assign

range (1)
template <class InputIterator>
  void assign (InputIterator first, InputIterator last);
fill (2)
void assign (size_type n, const value_type& val);
initializer list (3)
void assign (initializer_list<value_type> il);
Assign vector content

Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly.

给vector重新分配新的内容,替换现有的内容,并修改他的大小。


In the range version (1), the new contents are elements constructed from each of the elements in the range between first and last, in the same order.
在版本(1)中的范围,新的元素是从范围first到last的复制,顺序也是相同的。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> vi={10,20,30};
	vector<int> v2;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	v2.assign(vi.begin(),vi.end());
	cout<<"after assign,v2 elements is:";
	for_each(v2.begin(),v2.end(),[](int n){cout<<n<<" ";});
	cout<<endl;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	
	




}

截图:

In the fill version (2), the new contents are n elements, each initialized
to a copy of 
val.
在第二个请情况下,新的容器一共有n个元素,每一个元素都初始化为val的拷贝。
例子:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> v2;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	v2.assign(5,999);
	cout<<"after assign,v2 elements is:";
	for_each(v2.begin(),v2.end(),[](int n){cout<<n<<" ";});
	cout<<endl;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	
	




}

截图:

In the initializer list version (3), the new contents are copies of the values passed as initializer list, in the same order.
在列表初始化版本(3)中,新的元素的值时从初始化列表中依次复制过来的。

#include <iostream>
#include <vector>
#include <initializer_list>
#include <algorithm>
using namespace std;
int main()
{
	vector<int> v2;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	v2.assign({10,20,30,40});
	cout<<"after assign,v2 elements is:";
	for_each(v2.begin(),v2.end(),[](int n){cout<<n<<" ";});
	cout<<endl;
	cout<<"v2.size="<<v2.size()<<"   capacity="<<v2.capacity()<<endl;
	
	




}

结果:

The internal allocator is used (through
its 
traits) to allocate and deallocate storage
if a reallocation happens. It is also used to 
destroy all
existing elements, and to 
construct the new ones.
如果发生重分配,将使用其内部的分配器进行分配和释放存储空间,同时也用于析构旧的元素和构造新的元素。


Any elements held in the container before the call are destroyed and replaced by newly constructed elements (no assignments of elements take place).

所有的旧的元素依旧停留在容器内,直到该函数被调用将就的元素销毁并且替换成新的元素。


This causes an automatic reallocation of the allocated storage space if -and only if- the new vector size surpasses
the current vector capacity.

如果新的vector大小大于现在的capacity,那么将会自动进行重分配。


Parameters

first, last
Input iterators to the initial and final positions in a sequence. The range used is [first,last), which includes all the elements between first and last, including the element
pointed by first but not the element pointed by 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.

定义在同一个序列的输入型迭代器,分别标示了起始位置以及结束位置,包括所有的在first和last范围内的所有元素,包括first指向的元素但不包括last指向的元素。

该函数的模版参数InputIterator应该是一个输入迭代器。其指向一个能被value_type对象构造的元素。

n
New size for the container.

Member type size_type is an unsigned integral type.

容器新的大小。

类型为无符号整型.

val

Value to fill the container with. Each of the n elements in the container will be initialized to a copy of this value.

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).

填满容器的值。容器内的每一个元素都将从val中拷贝而来。

类型为元素类型,由vector的模版参数指定。

il
An initializer_list object. The compiler will automatically
construct such objects 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).

一个初始化列表。编译器将从initializer_list装饰器自动构造其元素。

其元素类型为容器元素类型。由vector模版定义.

Return value

none


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
// vector assign
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> first;
  std::vector<int> second;
  std::vector<int> third;

  first.assign (7,100);             // 7 ints with a value of 100

  std::vector<int>::iterator it;
  it=first.begin()+1;

  second.assign (it,first.end()-1); // the 5 central values of first

  int myints[] = {1776,7,4};
  third.assign (myints,myints+3);   // assigning from array.

  std::cout << "Size of first: " << int (first.size()) << '\n';
  std::cout << "Size of second: " << int (second.size()) << '\n';
  std::cout << "Size of third: " << int (third.size()) << '\n';
  return 0;
}


Output:

Size of first: 7
Size of second: 5
Size of third: 3



Complexity

Linear on initial and final sizes (destructions, constructions).

和容器起始的大小以及最终的大小线性相关。(析构,构造)

Additionally, in the range version (1), if InputIterator is not at least of a forward
iterator
 category (i.e., it is just an input iterator) the new capacity cannot be determined beforehand and the operation
incurs in additional logarithmic complexity in the new size (reallocations while growing).

此外,在范围版本(1),如果迭代器类型不至少是一个正向迭代器(例如,就是一个输入迭代器),新的容量不能被事先决定的话以及操作会导致额外的对数复杂度在增加新的大小的时候(这里不太通顺,应该是说如果超过了capacity,重分配会增加对数时间的复杂度)。



Iterator validity

All iterators, pointers and references related to this container are invalidated.

所有的迭代器,指针以及引用都将失效。


Data races

All copied elements are accessed.

所有被复制的元素都将被访问.

The container is modified.

容器将被修改。

All contained elements are modified.

容器内所有的元素都将被修改。


Exception safety

Basic guarantee: if an exception is thrown, the container is in a valid state.

如果抛出异常,容器将依旧处于有效状态。


If allocator_traits::construct is not supported with the appropriate arguments
for the element constructions, or if the range specified by[first,last) is not valid, it causes undefined behavior.

如果分配器不支持元素构造的参数,或者是[first,last)是无效的,将会导致未定义的行为。

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

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

2014-8-15

于GDUT




发表回复

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