实战c++中的vector系列--构造、operator=和assign区别

vector也许是实际过程中使用最多的stl容器,看似简单,其实有很多技巧和陷阱。

着重看一看vector的构造,暂时按照C++11:

default (1) 
explicit vector (const allocator_type& alloc = allocator_type());

fill (2)    
explicit vector (size_type n);
         vector (size_type n, const value_type& val,
                 const allocator_type& alloc = allocator_type());

range (3)   
template <class InputIterator>
  vector (InputIterator first, InputIterator last,
          const allocator_type& alloc = allocator_type());

copy (4)    
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);

move (5)    
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);

initializer list (6)    
vector (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

直接看看下面的代码,就知道如何构造一个vector了:

#include <iostream>
#include <vector>

int main ()
{

  std::vector<int> first;                                // default (1) 
  std::vector<int> second (4,100);                       // fill (2)
  std::vector<int> third (second.begin(),second.end());  // range (3)
  std::vector<int> fourth (third);                       // a copy of third

  // the iterator constructor can also be used to construct from arrays:
  int myints[] = {16,2,77,29};
  std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

  std::cout << "The contents of fifth are:";
  for (std::vector<int>::iterator it = fifth.begin(); it != fifth.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

===========================================================
vector重载了=运算符,也有一个叫assign的方法,而且有什么区别吗?

std::vector::operator=
直接代码:

#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> foo (3,0);
  std::vector<int> bar (5,0);

  bar = foo;
  foo = std::vector<int>();

  std::cout << "Size of foo: " << int(foo.size()) << '\n';
  std::cout << "Size of bar: " << int(bar.size()) << '\n';
  return 0;
}

//结果:
Size of foo: 0
Size of bar: 3

这里需要说明的是:
replacing its current contents
modifying its size accordingly

std::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;
}
//输出:
Size of first: 7
Size of second: 5
Size of third: 3

这里同样需要说明:
replacing its current contents
modifying its size accordingly

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一苇渡江694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值