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

來源:互聯網
上載者:User

標籤:方法   track   copy   append   main   java   current   stl   this   

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: 0Size 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: 7Size of second: 5Size of third: 3

這裡相同須要說明:
replacing its current contents
modifying its size accordingly

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.