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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL,map是一种关联容器,于存储键值对assign()是map类的一个成员函数,用于将一个map的内容替换为另一个map的内容。具体来说,assign()函数接受一个迭代器范围作为参数,将该范围内的元素赋值给当前的map。 请注意,assign()函数会将当前map的所有元素删除,并用参数范围内的元素来替换它们。这意味着,如果两个map的键值对类型不匹配,则无法使用assign()函数。 另外,需要注意的是,assign()函数在执行过程不会保留键值对的顺序,并且在插入具有相同键的元素时,新元素会覆盖旧元素。 例如,下面的代码演示了如何使用assign()函数: ``` std::map<int, std::string> map1; map1 = "apple"; map1 = "banana"; std::map<int, std::string> map2; map2 = "carrot"; map2 = "date"; map1.assign(map2.begin(), map2.end()); // 现在map1的内容为{3: "carrot", 4: "date"} ``` 总结起来,assign()函数可以用于将一个map替换为另一个map的内容,并且在执行过程会删除当前map的所有元素。同时,值得注意的是,assign()函数在插入具有相同键的元素时会进行覆盖操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [STLmap和string, vector 用法详解](https://blog.csdn.net/iteye_17686/article/details/82294016)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值