程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 實戰c++中的vector系列--構造、operator=和assign區別

實戰c++中的vector系列--構造、operator=和assign區別

編輯:C++入門知識

實戰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 
  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 il,
       const allocator_type& alloc = allocator_type());

直接看看下面的代碼,就知道如何構造一個vector了:

#include 
#include 

int main ()
{

  std::vector first;                                // default (1) 
  std::vector second (4,100);                       // fill (2)
  std::vector third (second.begin(),second.end());  // range (3)
  std::vector 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 fifth (myints, myints + sizeof(myints) / sizeof(int) );

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

  return 0;
}

===========================================================
vector重載了=運算符,也有一個叫assign的方法,而且有什麼區別嗎?

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

#include 
#include 

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

  bar = foo;
  foo = std::vector();

  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 
#include 

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

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

  std::vector::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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved