`
阿尔萨斯
  • 浏览: 4177090 次
社区版块
存档分类
最新评论

STL 中的assign方法(19)

 
阅读更多
public member function
<vector>

std::vector::assign

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

Assigns new contents to thevector, replacing its current contents, and modifying itssizeaccordingly.

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



In therange version(1), the new contents are elements constructed from each of the elements in the range betweenfirstandlast, 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 thefill version(2), the new contents arenelements, each initialized to a copy ofval.
在第二个请情况下,新的容器一共有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 theinitializer 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;
	
	




}
结果:




Theinternal allocatoris used (through itstraits) toallocateanddeallocatestorage if a reallocation happens. It is also used todestroyall existing elements, and toconstructthe new ones.
如果发生重分配,将使用其内部的分配器进行分配和释放存储空间,同时也用于析构旧的元素和构造新的元素。

Any elements held in the container before the call aredestroyedand 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 vectorsizesurpasses the current vectorcapacity.

如果新的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 betweenfirstandlast, including the element pointed byfirstbut not the element pointed bylast.

The function template argumentInputIteratorshall be aninput iteratortype that points to elements of a type from whichvalue_typeobjects can be constructed.

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

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

n
New size for the container.

Member typesize_typeis an unsigned integral type.

容器新的大小。

类型为无符号整型.

val

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

Member typevalue_typeis the type of the elements in the container, defined invectoras an alias of its first template parameter (T).

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

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

il
Aninitializer_listobject. The compiler will automatically construct such objects frominitializer listdeclarators.
Member typevalue_typeis the type of the elements in the container, defined invectoras 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 finalsizes(destructions, constructions).

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

Additionally, in therange version (1), ifInputIteratoris not at least of aforward iteratorcategory (i.e., it is just aninput iterator) the new capacity cannot be determined beforehand and the operation incurs in additional logarithmic complexity in the newsize(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.

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


Ifallocator_traits::constructis not supported with the appropriate arguments for the element constructions, or if the range specified by[first,last)is not valid, it causesundefined behavior.

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

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

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

2014-8-15

于GDUT




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics